Input the gender of 10 students in the form of m, M or f, F. Display how many students are male and female. Use for loop and while loop.
#include <iostream>
using namespace std;
int main(){
int totalFemale=0;
int totalMale=0;
for(int s=0;s<10;s++){
char gender=' ';
while(gender!='f' && gender!='F' && gender!='m' && gender!='M'){
cout<<"Select gender for student "<<(s+1)<<" (f,F - female, m,M - male): ";
cin>>gender;
}
if(gender=='f'|| gender=='F'){
totalFemale++;
}else{
totalMale++;
}
}
//display result
cout<<"The number of female students in class: "<<totalFemale<<"\n";
cout<<"The number of male students in class: "<<totalMale<<"\n\n\n";
system("pause");
return 0;
}
Comments
Leave a comment