continuous loop cannot stop

Hi guys, I'm a beginner in C++ (2 days only). I'm studying the tutorial doc in this website, and have currently read the Introduction portion to Statements and Flow Control portion (and a few forum posts).

So I was practicing the things that I have learned so far, and I came across a wall. I saw this break/continuous program sample in the tutorial (10,9,8... Lift Off!) and thought to tweak it a bit with the limited things that I currently know.

(1) I wanted the user to input a number from 5 to 10 only, which will start off the countdown to ... (BOOM);

(2) I wanted to make safety precautions that if the user inputs a number below 5 (including negative numbers) and above 10, he/she will be prompted to try again by entering a number within the correct range;

(3) I wanted to practice with stringstream, by ensuring that what he/she entered is (or would be converted) to an erroneous entry if the user enters characters instead of numbers.

PROBLEM: So the remaining problem is when I run the program. It would appear that the program works fine EXCEPT when the user enters a number out of range (below 5 or above), which will then prompt him/her to enter a correct number, and if the user follows it up with a CHARACTER INPUT, it all goes wacky from there (continuous loop).

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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
	
	long long int i;
	string mystring;
	mystring = " ";
	
	while(true){	
	cout << "Please enter a number between 5 and 10 only: " << flush;
	getline(cin,mystring);
	cout << endl;
	stringstream mystream(mystring);
	if(mystream>>i)
		break;
		cout << "Invalid input: " << flush;
	}
		
	while (i>10){
		cout << "Too high, please enter a number between 5 and 10 only:";
		cin >> i;
		cout << endl;
	}
		
	while(i<5 ||i<0){
		cout << "Too low, please enter a number between 5 and 10 only: ";
		cin >> i;
		cout << endl;
	
	}
			
	if (i<=10 && i>=5){
		do{
			cout << i << ", " << flush;
			i=i-1;
		}while(i>0);
		
		cout << "BOOM" << endl;	
	}

	cin.get();
	return 0;
}


Thanks
Last edited on
Get rid of lines 25 and 31, and make lines 29 and 23 if statements instead of while loops. Your input is performed on line 15 each iteration of the loop.
Last edited on
Topic archived. No new replies allowed.