Code error help please

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;
}
Please Edit your post and put all of your code in code tags - http://www.cplusplus.com/articles/jEywvCM9/

Also, please copy paste the errors you are having.
I've read your code but i found it more comfortable to code this program by myself.... Here is my code for your program. I've used Visual Studio 2012 Compiler and there were no errors detected in my code... Hope you find it helpful...!!!


Use 'm', not 'M', for male and 'f' for female...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	char gender;
	double scoredgpa=0;
	double totalmalegpa=0;
	double totalfmalegpa=0;
	int m=0,f=0;

do
	{
	cout << "Enter Gender: ";
	cin >> gender;

	cout << "Enter GPA scored: ";
	cin >> scoredgpa;

	if(gender=='m')
		{
			m+=m++;
			totalmalegpa+=scoredgpa;
		}

	else if(gender=='f')
		{
			f+=f++;
			totalfmalegpa+=scoredgpa;
		}

	cout << endl << endl;   // just for easy-to-read output.
	
	cout << "Press 'q' if you wish to add more enteries...!!!" << endl << endl;

	}	// this is the closing bracket of do-while loop...
while (getch()=='q');


	double maleavg;   
	double fmaleavg; 

	maleavg=totalmalegpa/m; 
	fmaleavg=totalfmalegpa/f;  

	cout << "The average GPA scored by males is: " << maleavg << endl;
	cout << "The average GPA scored by females is: " << fmaleavg << endl;

	
	system ("pause");
}


The output is something like:

Enter Gender: f
Enter GPA scored: 3.67

Press 'q' if you wish to add more entries...!!!

Enter Gender: f
Enter GPA scored: 2.52

Press 'q' if you wish to add more entries...!!!

Enter Gender: m
Enter GPA scored: 3.33

Press 'q' if you wish to add more entries...!!!

The average GPA scored by males is: 3.33
The average GPA scored by females is: 3.095
Last edited on
Hello using your code I get this error

1>------ Build started: Project: Intermediate23 Project, Configuration: Debug Win32 ------
1> Intermediate23.cpp
1>c:\cpp8\chap 07\intermediate23 project\intermediate23 project\intermediate23.cpp(39): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\conio.h(131) : see declaration of 'getch'
1> Intermediate23 Project.vcxproj -> C:\Cpp8\Chap 07\Intermediate23 Project\Debug\Intermediate23 Project.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/

if(ch=='M')else if(ch=='F')
What if the user enters 'f' or 'm' for their gender?

 
m=0;f=0;mgpa=0;fgpa=0;

Why not initialise them as you declare them?

if(gender=='m') else if(gender=='f')
What if the user enters 'M' or "F' ?

while (getch()=='q');
Might want to re-check this.

1
2
3
4
5
6
7
8
double maleavg; 
double fmaleavg; 

maleavg=totalmalegpa/m; 
fmaleavg=totalfmalegpa/f; 

cout << "The average GPA scored by males is: " << maleavg << endl;
cout << "The average GPA scored by females is: " << fmaleavg << endl;


Why not just this:

1
2
cout << "The average GPA scored by males is: " << totalmalegpa/m << endl;
cout << "The average GPA scored by females is: " << totalfmalegpa/f<< endl;


m=m++;f=f++;
should be just
m++;f++;
Last edited on
@tennisnash2 ... make sure you are including the <conio.h> header library. if not, then declaration of getch() is not done....

i've made some changes in my code.

use:
1
2
m+=m++
f+=f++ 


instead of:
1
2
m=m++
f=f++ 
(The second case works fine in Visual Studio but in case you are using any other compiler, then this might not work in all of them.)
Last edited on
@integralfx.... you can use 'f' or 'F' or any other character. But for that you will just have to change the character that you have to put in if and else-if cases. Like, if you want to the user to enter 'F' for female gender and 'M' for male. You just have to make the following simple change:

if(gender=='M') else if(gender=='F')
You can use any character by putting the desired character inside the single commas.


There is no problem with (getch()=='q')
i've checked this using both Visual Studio and Dev C++ Compilers. And i've been using this from quite a long time. The only thing you need to make sure is that you have to include <conio.h> header library.


You can find the average by using the code you mentioned...
cout << "The average GPA scored by males is: " << totalmalegpa/m << endl;


Its OK if you just use
m++; f++;

But this won't work here because you need to store the sum of increment after each iteration, in order to find the average. That's why i declared the variables 'm' and 'f', and used them to store the sum of all the increments occured in 'm++' and 'f++' respectively.
So later on it helped me finding the average like you mentioned;
totalmalegpa/m

You cannot write;
totalmalegpa/m++
This clearly creates a logical and most probably a syntax error as well.
Last edited on
Topic archived. No new replies allowed.