why there are 2 getlines in the string name? (what will happen if only one?) - since there is an if statement, why is there no else statement?
In the C++ code below, what is the explanation:
- why there are 2 getlines in the string name? (what will happen if only one?)
- since there is an if statement, why is there no else statement?
#include <iostream>
using namespace std;
int main()
{
int n;
// uer input
cout << "Enter number of students: ";
cin >> n;
string gender[n], name[n];
// input for name and gender
for (int i = 0; i < n; i++)
{
cout << "Enter gender of Student: ";
cin >> gender[i];
cout << "Enter name of Student: ";
getline(cin, name[i]);
getline(cin, name[i]);
}
cout << "---------------------" << endl;
cout << "List of Female Students" << endl;
// printing female students
for (int i = 0; i < n; i++)
{
if (gender[i] == "Female")
cout << name[i] << endl;
}
cout << "List of Male Students" << endl;
cout << "---------------------" << endl;
// printing male students
for (int i = 0; i < n; i++)
{
if (gender[i] == "Male")
cout << name[i] << endl;
}
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps