how to get c++ to read multiple characters in one input?

Write a C++ program that reads in letter grades for a class. Acceptable grades are A, B, C, D, and F. The user may enter as many grades as desired and enters a Z to indicate the end of the grades. The terminating letter Z does not count as a grade. No plus or minus should be attached to a grade. The program should ignore any other characters entered by the user. Your program should treat a lower-case letter like its upper-case equivalent; for example, process a b as if it were a B.

The program should keep track of the number of students that passed (D or above) and the number that failed (F). After the user finishes entering the letter grades, the program should calculate and print the percentage of students passing and failing as well as the class grade-point average (GPA) (A = 4.0, B = 3.0, C = 2.0, D = 1.0, F = 0.0).

Any letters that the user enters that are not A, B, C, D, or F do not participate in the calculation of the pass/fail percentage and do not contribute to the GPA calculation. If the user enters a Z before entering any valid letter grades, the program should terminate without printing anything.

Sample run:

Enter grades (Z terminates the list): A B B C F D Z
Students passing: 5 (83.3333%)
Students failing: 1 (16.6667%)

Class GPA: 2.16667

-----------------------------------------

this assignment shouldnt be that hard but for some reason i cant figure out how to get the program to read as many inputs as the user wants to put in. i think i can do the rest after that.
Last edited on
Maybe you can use

1
2
3
4
5
6
7
8
9
char grade;

while(cin >> grade)
{
...
  if(grade == 'Z')
    break;
...
}

?

I can think of more than one way to implement that
Last edited on
Just one addition, you need to do
1
2
if( toupper(grade)  == 'Z')
    break;


Since you need to accept lower and uppercase letters and treat them same.
Same when you compare other grades and increase their count.
thanks! i'm going to try that out.
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
#include <iostream>
using namespace std;

int main()
{
	char grade;
	int i=0, j=0;
	float  input=0, sum=0, avg;
		while (cin>>grade)
		{
			if (toupper(grade)=='A')
			{
				i++;
				input=4;
			}
			sum += input;
			if (toupper(grade)=='B')
			{ 
				i++;
				input=3;
			}
			sum += input;
			if (toupper(grade)=='C')
			{ 
				i++;
				input=2;
			}
			sum += input;
			if (toupper(grade)=='D')
			{ 
				i++;
				input=1;
			}
			sum += input;
			if (toupper(grade)=='F')
			{ 
				j++;
				input=0;
			}
			sum += input;
			if (toupper(grade)=='Z')
			{	
				break;
			}
		}

	cout << sum << endl;
	avg= sum/i;
	cout << i << "," << j << endl;
	cout << avg;
}


this is what i have done so far.. but my sum is always coming out super high?? what am i doing wrong?
Last edited on
You're adding input to sum 5 separate times for every loop iteration?

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
		while (cin>>grade)
		{
			if (toupper(grade)=='A')
			{
				i++;
				input=4;
			}
			else if (toupper(grade)=='B')
			{ 
				i++;
				input=3;
			}
			else if (toupper(grade)=='C')
			{ 
				i++;
				input=2;
			}
			else if (toupper(grade)=='D')
			{ 
				i++;
				input=1;
			}
			else if (toupper(grade)=='F')
			{ 
				j++;
				input=0;
			}
			else if (toupper(grade)=='Z')
			{	
				break;
			}
			sum += input;
		}
oh yea that makes sense.
Topic archived. No new replies allowed.