loop counting

I'm having trouble counting each individual grade in a loop. Like how many A's, B's, etc. but I can get it to count all of the grades. Any help would be awesome! Thanks!

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
54
55
56
57
58
59
60
61
62
63
64
65
  #include <iostream>
using namespace std;

int getGrade();
char getLetter();

int main()
{
	int count = 0;
	while(getLetter()!='Q')
	{
		count++;
	}
	cout<<count<<endl;
}


int getGrade()
{
	int grade= 0;
	cout<< "-1 to quit"<<endl;
	cout << "what is grade"<<endl;
	cin >> grade;
	return grade;
}

char getLetter()
{
	char letter;
	int grade;
	grade=getGrade();
	if(grade ==-1)
	{
		cout<< "you choose to quit"<<endl;
		letter = 'Q';
	}
	 else if(grade >= 91)
	{
		cout << "Grade is an A"<<endl;
		letter = 'A';
	}
	else if(grade>=81)
	{
		cout<< "Grade is a B"<<endl;
		letter = 'B';
	}
	else if(grade>=71)
	{
		cout << "Grade is a C"<<endl;
		letter = 'C';
	}
	else if(grade>=65)
	{
		cout << "Grade is a D"<<endl;
		letter = 'D';
	}

	else 
	{
		cout << "Grade is a F"<<endl;
		letter = 'F';
	}
	return letter;

}
You need to use different counters for each letter. And you would use a switch() for this, and also, save the letter each time you read it.

A somewhat more elegant and shorter approach would be to use std::map.
http://www.cplusplus.com/reference/map/map/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <map>

using namespace std; // bad habit, try to unlearn it

int main()
{
	int count = 0;
	char c;
	map<char, int> grades;

	while((c = getLetter()) != 'Q')
	{
		grades[c]++;
		count++;
	}

	cout << "Grade B apeared " << grades['B'] << " times.\n";
	cout<<count<<endl;
}


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int count[6] = {0};
char letter;

do
{
    letter = getLetter();
    count[int(letter - 'A')]++;
}while(letter!='Q');

cout << endl;

for( int i=0; i<6; i++)
{
    if( count[i] != 0) //Dont print grades with zero count
        cout << char('A' + i) << ':' << int(count[i]) << ", ";
}
This is an assignment for class and we really haven't gotten into arrays yet but thanks for the feedback! CatFish, I tried using the switch statement but I'm only getting a count of 1 for each grade when I have put in multiples.
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
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	int d = 0;
	int f = 0;
	while(getLetter()!='Q')
	{
		switch(getLetter())
		{
			case 'A':
			a++;
		break;
			case 'B':
				b++;
				break;
			case 'C':
				c++;
				break;
			case 'D':
				d++;
				break;
			case 'F':
				f++;
				break;
		}




		
	}
	cout<<a<<b<<c<<d<<f<<endl;
}
For some odd reason I feel like it has to do with my functions??
That's why I mentioned that you should save the letter you read (as I did in my example), otherwise you'll be reading it more times.

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
	char c;

	while((c = getLetter()) != 'Q')
	{
		switch (c)
		{
			case 'A':
			a++;
		break;
			case 'B':
				b++;
				break;
			case 'C':
				c++;
				break;
			case 'D':
				d++;
				break;
			case 'F':
				f++;
				break;
		}




		
	}
oh man your're a life saver! Thank you!
Topic archived. No new replies allowed.