Need pointers with loops please

I have been trying to get the loops after line 25 to exit properly. Once I get into the loops it will just keep asking for inputs and not post any messages back. I have spent the last few hours trying to get it but to no avail. I need some pointers from you guys please.

Here is the code:
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int numberOfStudents;
	cout << "How many students in the class? ";
	cin >> numberOfStudents;

	int i; // initial value
	int sum = 0;
	int score = 0;
	for (i = 1; i <= numberOfStudents; i++)
	{
		cout << "Enter score between 0 and 100 for student #" << i << ": ";
		cin >> score;
		sum += score;
	}

	cout << "Average Score: " << fixed << setprecision(2) << sum / (i - 1.00) << endl;


	cout << endl;

  char choice;
	do
	{
		int courseID;
		cout << "Enter 3-digit course ID: ";
		cin >> courseID;
		cout << endl;

		char grade;
	  cout << "Enter letter grades A-C (X to quit): ";
		cin >> grade;

		int sumA = 0;
		int sumB = 0;
		int sumC = 0;

		while (grade != 'X' || 'x')
		{
			if (grade == 'A' || 'a')
				sumA++;
			else if (grade == 'B' || 'b')
				sumB++;
			else if (grade == 'C' || 'c')
				sumC++;
			else
				cout << "Invadid letter grade. Data ignored." << endl;
		
		  cin >> grade;
		}

  cout << "Class " << courseID << " had " << sumA << " A(s), " << sumB << " B(s), " << sumC << " C(s)." << endl;

	cout << "Do you wish to enter the grades for another course? (Y/N)? ";
	cin >> choice;
	} while (choice != 'N' || 'n');

  return 0;
}


Here is what I want it to do:

How many students in the class? 5
Enter score between 0 and 100 for student #1: 73
Enter score between 0 and 100 for student #2: 97
Enter score between 0 and 100 for student #3: 0
Enter score between 0 and 100 for student #4: 82
Enter score between 0 and 100 for student #5: 85
Average Score: 67.40
High Score: 97.00
Low Score: 0.00
Enter 3-digit course ID: 210
Enter letter grades A-C (X to quit): a
A
b
B
k
Invalid letter grade. Data ignored.
B
c
x
Class 210 had 2 A(s), 3 B(s), and 1 C(s).
Do you wish to enter the grades for another course? (Y/N) y
Enter 3-digit course ID: 142
Enter letter grades A-C (X to quit): c
C
C
X
Class 142 had 0 A(s), 0 B(s), and 3 C(s).
Do you wish to enter the grades for another course? (Y/N) N

Thanks again!
while (choice != 'N' || 'n'); means nothing. You're looking for while(choice != 'N' || choice != 'n');.

[edit]

Also counts for all your ifs. With your mistake, the things being checked is:
if "(choice == N) != 0" or "n != 0". The latter is always true, because 'n' is not 0 (it's in fact a non-zero constant). Thus, every check will return "true".
Last edited on
while (choice != 'N' || 'n');

This says that your loop will run as long as the following is true:

choice does not equal N
OR
n (This is NOT choice does not equal n - it is just plain n)

Now, in C++ anything that isn't zero is considered true for these purposes, so we can rewrite this as

choice does not equal N
OR
true

So this will always, always come out as true and the loop will run forever.
Last edited on
Topic archived. No new replies allowed.