Solving infinite loop

I am below 18 and I learn c++ with myself , I need some help on basic programming of c++ . Thanks . Question is the second line . I get this code from a book and I have solve the other question except this .

/*
*Revise the program so that it keeps playing the game until the user wants to quit.(Question ^^)
*(Hint : Add another loop around the existing loop)
*
*/

#include <iostream>
using namespace std ;

int main()
{
int total = 0 , n = 0;

cout << "welcome to NIM .Pick a starting point ."<<endl;
cin >> total ;
while(total <= 0){
cout << "Starting point must be bigger than 0 "<<endl;
cout <<"Re-enter : ";
cin >> total ;
}

while (true)
{
if ((total % 3) == 2){
total = total - 2;
cout << "I am substracting 2 ."
}else{
total -- ;
cout << "I am substracting 1 ."
}

cout << "New total is " << total <<endl;
if (total <= 0 ){
cout << "I win " <<endl;
break;
}

cout <<"Enter a num to substract (1 or 2) : "
cin >> n;
while ( n < 1 || n > 2) {
cout <<"Input must be 1 or 2 ."<<endl;
cout << "Re-enter : ";
cin >> n ;
}
total = total - n;
cout << "New total is "<< total <<endl;
if (total <= 0){
cout << "You win !" <<endl;
break;
}

}
return 0;
}
Last edited on
The program must keep restarting until unless the user says otherwise.
Example:
I want to keep displaying a certain message until the user has entered -99.

Here's how I'll do it:

1
2
3
4
5
6
7
int i=0;
while(i!=-99)
{
cout << "Certain Message!" << endl;
cout << "Enter option:" << endl;
cin >> i;
}




Try to improve and use a character type variable to store the user's command.
Q or q to quit the program.
char command = '\0';

1
2
3
4
5
6
7
8
9
while(c!='Q' || c!='q')
{
 = = = = 
 = = = = 
   CODE
 = = = =
 = = = =
cin >> command;
}


I hope I was helpful.
Thanks ! I did't thought that have this way !
I'm glad I could help.
Taha ilyas where did you learn to code in C++? I am looking for good book!
Taha ilyas^^ , how to do the second suggestion . Using 'c' as exit .

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
#include <iostream>
using namespace std ;

int main()
{
	int total = 0 , n = 0 ;
	char c ;
	
	cout << "Welcome to NIM "<<endl;
	
	cout << "Enter any letter to continue or enter 'q' or 'Q' to quit ." <<endl;
	cin >> c;
	while( c != 'q' || c != 'Q')
{
	cout << "Pick a starting point ."<<endl;	
	cin >> total ;
	while(total <= 0){
		cout << "Starting point must be bigger than 0 "<<endl;
		cout <<"Re-enter : ";
		cin >> total ;
	}
	
   while (true)
   {
	
		if ((total % 3) == 2){
			total = total - 2;
			cout << "I am substracting 2 .";
		}else{
			total -- ;
			cout << "I am substracting 1 .";
		}
		
		cout << "New total is " << total <<endl;
		if (total <= 0 ){
			cout << "I win " <<endl;
			break;
		}
		
		cout <<"Enter a num to substract (1 or 2) : ";
		cin >> n;
		while ( n < 1 || n > 2)	{
			cout <<"Input must be 1 or 2 ."<<endl;
			cout << "Re-enter : ";
			cin >> n ;
		}
		total = total - n;
		cout << "New total is "<< total <<endl;
		if (total <= 0){
			cout << "You win !" <<endl;
			break;
		}
	}
	cout << "Enter 'q' or 'Q' to quit or press others letters to continue."<<endl;
	cin >> c;
}
return 0;	
}
Last edited on
I have solved trying the first suggestion . But the second is buggy and mess...
Line 13: You want &&, not ||. If c is 'q', then c!='Q' will be true causing you to execute the loop.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Thanks ^^
Topic archived. No new replies allowed.