C++ Program Help

Create a program that allows the user to enter the gender (either F or M) and GPA (0.0 through 4.0) for any number of students. The program should calculate and display the average GPA for all students, the average GPA for male students, and the average GPA for female students.



#include <iostream>
using namespace std;
int main(void)
{
char ch;
float gpa,mgpa,fgpa;
int m,f;
m=0;f=0;mgpa=0;fgpa=0;
cout<<"Enter gender and gpa(q to quit):";
while(true)
{
cin>>ch;
if(ch=='q')
{
break;
}
cin>>gpa;
if(ch=='M')
{
m++;
mgpa+=gpa;
}
else if(ch=='F')
{
f++;
fgpa+=gpa;
}
else
{
cout<<"unknown input"<<endl;
}
cout<<"Enter gender and gpa(q to quit):";
}
if(m>0)
{
cout<<"The average gpa for male students is "<<mgpa/m<<endl;
}
else
{
cout<<"There are no male students"<<endl;
}
if(f>0)
{
cout<<"The average gpa for female students is "<<fgpa/f<<endl;
}
else
{
cout<<"There are no female students"<<endl;
}


return 0;
}
Last edited on
Topic archived. No new replies allowed.