New to c++ while loop functions

I'm to write a program using function to ask the user to enter their grade in a class, then determine the GPA. All the while keeping track of the number of people who have passed and failed.

I cannot seem to get the while loop to accept multiple entry's until ctrl z is entered. I think I may have the (!cin.eof()) in the wrong place. Any help would be greatly appreciated.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;



void showTitle()
{
cout << "Student Grade Summary" << endl;
cout << "Enter Grades A, B, C, D, F or Ctrl Z when done" << endl;
}

char getValidGrade()
{
char grade;
int numstudentpass , numstudentfail;

cout << "Enter a grade: ";
cin >> grade;
grade=toupper(grade);



while ((!cin.eof()) && grade !='A' && grade != 'B' && grade != 'C' && grade != 'D' && grade !='F')
{
cout << "Enter a,b,c,d,f or Ctrl Z only" << endl;
cin >> grade;
grade=toupper(grade);

if (grade !='f')
{
numstudentpass ++;
}
else
{
numstudentfail ++;
}




return grade;
}

}

double determineGPA(char grade)
{

double gpa;

switch (grade)
{
case'A':
gpa= 4.0;
break;
case'B':
gpa= 3.0;
break;
case'C':
gpa= 2.0;
break;
case'D':
gpa= 1.0;
break;
default:
gpa=0.0;
}

return gpa;
}

void showGPA (double gpa,char grade)
{
ofstream fout ("grades.dat");

cout << "Your GPA is " << gpa << endl;
cout << "Your Grade is " << grade << endl;
}


int main()
{

ofstream fout ("grades.dat");
if (!fout.is_open())
{
cout << "Can't open file - Contact systems" << endl;
system ("pause");
exit (666);
}

char grade;
double gpa;
int numstudentpass , numstudentfail;

showTitle();

grade=getValidGrade();

gpa=determineGPA(grade);

showGPA(gpa,grade);



system ("pause");
}
There are a few things.

This isn't working correctly:
while ((!cin.eof()) && grade !='A' && grade != 'B' && grade != 'C' && grade != 'D' && grade !='F')

try something like while (!cin.eof()) && (grade == 'A' || grade == 'B'...))

Then you put the letter to upper, so your if statement never calculates 'f'. You increase the pass/fail count, but this is a local variable and lost when the function ends.

But the main thing is that you return grade while in the loop, so the loop will never loop.

And, grade is never initialized, try a do while loop.

If I don't return grade I get an error telling me that the getvaildgrade function must return a value
Here is your code formatted:
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
char getValidGrade()
{
	char grade;
	int numstudentpass , numstudentfail;

	cout << "Enter a grade: ";
	cin >> grade;
	grade=toupper(grade);



	while ((!cin.eof()) && grade !='A' && grade != 'B' && grade != 'C' && grade != 'D' && grade !='F')
	{
		cout << "Enter a,b,c,d,f or Ctrl Z only" << endl;
		cin >> grade;
		grade=toupper(grade);

		if (grade !='f')
		{
			numstudentpass ++;
		}
		else
		{
			numstudentfail ++;
		}




		return grade;  // Bad
	}

     // return grade;  // It goes here, but you have a bunch of other problems too
}

}  //  Too many brackets also 


you return grade from inside the while loop, so the loop never loops.
Topic archived. No new replies allowed.