Having trouble finding the error in my code (ft. while loops)

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()


{
	char name;
	int gradeOne, gradesTwo, gradesThree, gradeAverage;

    cout << "What is your name?" << endl;
	cin >> name;

	cout << "You will be asked for 3 grades!" << endl;
	
	cout << "What is your first grade?" << endl;
	cin >> gradeOne;

		while(gradeOne > 0 && gradeOne < 101)
		{
			cout << "Invalid: Grades must be between 1 & 100! Please Re-enter." << endl;
			cin >> gradeOne;
		}

	cout << "What is your second test score?" << endl;
	cin >> gradeTwo;
		
		while(gradeTwo > 0 && gradeTwo < 101)
		{
			cout << "Invalid: Grades must be between 1 & 100! Please Re-enter." << endl;
			cin >> gradeTwo;
		}

	cout << "What is your Third test score?" << endl;
	cin >> gradeThree;

		while(gradeThree > 0 && gradeThree < 101)
		{
			cout << "Invalid: Grades must be between 1 & 100! Please Re-enter." << endl;
			cin >> gradeThree;
		}
	
	gradeAverage = (gradeOne + gradeTwo + gradeThree) / 3;
	
	cout << name << endl;
	cout << testAverage << end;

return 0;
}

getting an overload error
Last edited on
You are storing name in a char, which can contain exactly one character. So, if you try to type any more than one character, say, "Bobby", name will have the value 'B' and the input stream will still have "obby" waiting for input.

When you come to line 18, cin takes your input stream ("obby") and tries to turn it into an int, which it can't. This sets a failure flag, so from then on calling std::cin will fail automatically, meaning your while loops never finish.

Luckily, there is an easy fix: change line 9 so that name is of type std::string rather than char; this will allow it to take more than one character. Also, you can clear the failure flag so that even if the user gives letters rather than numbers, your program won't just die:
1
2
3
4
5
6
7
8
9
10
#include <limits>
//...
while (testOne > 0 && testOne <= 100) {
    // ignore the rest of the input stream
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    // clear the error flag
    std::cin.clear();

    // ...
}


EDIT:
You changed your variable names while I was typing up my answer... Make sure that you type them right (i.e. gradeTwo rather than gradesTwo)
Last edited on
closed account (48T7M4Gy)
In function 'int main()': 
27:9: error: 'gradeTwo' was not declared in this scope 
36:9: error: 'gradeThree' was not declared in this scope
47:10: error: 'testAverage' was not declared in this scope 
10:16: warning: unused variable 'gradesTwo' [-Wunused-variable] 
10:27: warning: unused variable 'gradesThree' [-Wunused-variable]



Having trouble finding the error in my code (ft. while loops)

Found 'em, there they are. Well except for the overload whatever that is. :)
Last edited on
Topic archived. No new replies allowed.