Why won't this validate my input for values between 0-10?

Hi,
I am having trouble with this input validation code. When I input values of the designated range (0-10) the first time through this loop it allows the program to continue down as it should, but when I enter an input not in the range like -1, 11, or a it invalidates it and prompts me to reenter. At this point when I enter a number that is 0-10 it still invalidates it and prompts me to reenter. Stays in an infinite loop. Can't figure it out. Please help!
(65-72)

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int i1 = 0;
	int i2 = 1;
	double score = 0;
	double scorenew = 0;
	double highestscore;
	double lowestscore;
	string name;
	string city;
	char prompt;
	double dod = 0;
	double overall;
	double average;
	double totaloverall = 0;
	int s = 1;


	cout << "Diving competitor's scores/information organizer" << endl;

	cout << "Enter information of diver? ";

	cin >> prompt;

	cout << endl;

	if (prompt != 'y')
	{
		return 0;
	}
	else

		cout << "Enter the divers name: ";

	cin >> name;

	cout << endl;

	cout << "Enter the diver's city: ";

	cin >> city;

	cout << endl;

	//prompt user to enter values

	while (prompt == 'y')//if user enters "y" the logic within is executed
	{
		i2 = 1;
		score = 0;
		highestscore = 0;
		lowestscore = 10;
		while (i2 <= 5)
		{

			cout << "Enter score given by judge #" << i2 << "\t";

			cin >> scorenew;

			while (!(0 <= scorenew && scorenew <= 10) || cin.fail())
			{
				cout << "Invalid. Please enter a number 1-10\n";
				cin.clear();
				cin.ignore(20, '\n');
			}

			cout << "\n";

			//input validation for score 



			score = score + scorenew;

			cout << score << "\n";

			i2 = i2 + 1;

			cout << i2 << "\n";

			if (scorenew >= highestscore)
			{
				highestscore = scorenew;
			}
			if (scorenew <= lowestscore)
			{
				lowestscore = scorenew;
			}


			cout << highestscore << "\n";
			cout << lowestscore << "\n";
		}

		cout << "Enter degree of difficulty: ";

		cin >> dod;


		cout << endl;




		overall = (score - (highestscore + lowestscore))*dod;

		cout << "Diver: " << name << ", City: " << city << "\n\n";

		cout << "Overall score was: " << overall << "\n";

		i1 = i1 + 1;

		totaloverall = totaloverall + overall;

		cout << "\n";

		cout << totaloverall;

		cout << "Enter information of diver? ";

		cin >> prompt;

		cout << endl;

		average = totaloverall / i1;

		cout << "\tEvent Summary" << "\n\n";

		cout << "Number of divers participating: " << i1 << "\n\n";

		cout << "Average score of all divers: " << average;

	}

	system("pause");

	return 0;


}
Last edited on
did it compile ?
everything compiles. I cut this out of a larger bit of code. I thought this would be more appropriate. Should I post all the code?
as i see your above code: i2 is not declared, and return0; --> return 0;
Between lines lines 63-72 is where I am having trouble. This part
1
2
3
4
5
6
7
8
9
10
cin >> scorenew;

			while (!(0 <= scorenew && scorenew <= 10) || cin.fail())
			{
				cout << "Invalid. Please enter a number 1-10\n";
				cin.clear();
				cin.ignore(20, '\n');
			}

			cout << "\n";
Last edited on
You never never try to get a new value for scorenew from cin inside the loop.
I know some folks don't like the

1
2
3
4
do
{ 
     //some statements;
}while (condition)


construction, but in this case it could be a good thing. Put your conditions after the block, and put this:

1
2
cout << "Enter score given by judge #" << i2 << "\t";
cin >> scorenew;


inside the block. Use a logical flag bool retry = false; before the block, and use it to decide whether to print the error message or not by setting it true inside the block:

like this:

1
2
3
4
5
6
7
8
9
bool retry = false // first run through the loop you get a free pass; no error

do 
{ 
      if (retry) cout << "You are wrong; please try again" << endl;
      retry = true; // after first time you get the error message until you get it right.
      cout << "Enter a valid. . .";
      cin >> value_being_entered;
}while (conditions_not_met);


The error message will only display on second and following retries.
Thank you PCrumley48, Peter 87, and anup30 for your helpful comments.
Topic archived. No new replies allowed.