Restart function with while loop

Jun 9, 2009 at 2:21am
This is my code, what I want to do is insert a function that restarts if the user presses anything besides 1-5.


#include <windows.h>
#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;

int main()
{
int difficulty;
int select;
int x;





system("COLOR 1a");
cout << " [1]Play Bruiser: Bruiser Origins " << "\n";
cout << " [2]Story \n";
cout << " [3]Game Options " << endl;
cout << " [4]Extras " << endl;
cout << " [5]Quit the game" << endl << endl;
cout << " Press any number 1-5 and press enter." << "\t";
cin >> select;

switch(select)
{
case 1:

cout << "This is the C++ text based game, not the actual game." << endl << endl;
cout << "Press any key to quit.";
break;

case 2:
cout << "Work in progress" << endl << endl;
cout << "Press any key to quit.";
break;

case 3:
cout << "You can't do anything" << endl << endl;
cout << "Press any key to quit.";
break;
case 4:
cout << "Beat the game first..." << endl << endl;
cout << "Press any key to quit.";
break;
case 5:
cout << "Haha, you figured out there is nothing to do!" << endl;
cout << "Press any key to quit.";
break;
default:
cout << "You're messing with me aren't you?"<< endl;
cout << "*sigh*...";



}
getch();
return 0;

}









I know that this isn't an actual game yet, my problem is, i'm not great with while and do while loops, could someone point me in the right direction?
Jun 9, 2009 at 2:52am
Put a while(true) loop around the code you want to repeat, then if you want to quit, just use a break.

If you need/want more info:
http://www.cplusplus.com/doc/tutorial/control/

Oh, by the way, please try to not use system() or <conio.h>...
Last edited on Jun 9, 2009 at 2:57am
Jun 9, 2009 at 3:17pm
PLEASE USE /CODE TAGS

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

#include <windows.h>
#include <iostream>
#include <conio.h>
#include <string.h>

using namespace std;

int main()
{
int difficulty;
int select;
int x;


int num;

do{
system("COLOR 1a");
cout << " [1]Play Bruiser: Bruiser Origins " << "\n";
cout << " [2]Story \n"; 
cout << " [3]Game Options " << endl;
cout << " [4]Extras " << endl; 
cout << " [5]Quit the game" << endl << endl;
cout << " Press any number 1-5 and press enter." << "\t";
cin >> select;

switch(select)
{
case 1:

cout << "This is the C++ text based game, not the actual game." << endl << endl;
cout << "Press any key to quit.";
break;

case 2:
cout << "Work in progress" << endl << endl;
cout << "Press any key to quit.";
break;

case 3:
cout << "You can't do anything" << endl << endl;
cout << "Press any key to quit.";
break;
case 4:
cout << "Beat the game first..." << endl << endl;
cout << "Press any key to quit.";
break;
case 5:
cout << "Haha, you figured out there is nothing to do!" << endl;
cout << "Press any key to quit.";
break;
default:
cout << "You're messing with me aren't you?"<< endl;
cout << "*sigh*...";
cout<<"Input Number not equal to 1 - 5 :";
cin>>num;
}while( // This Loop Condition can Probably Be simplified but you get the idea
num>=0&&
num!=1||
num!=2||
num!!=3||
num!=4||
num!=5);

} 
getch(); 
return 0;

}
Jun 9, 2009 at 4:33pm
}while( // This Loop Condition can Probably Be simplified but you get the idea
num>=0&&
num!=1||
num!=2||
num!!=3||
num!=4||
num!=5);


That's just:
}while ( ( num < 1 ) || ( num > 5) )

Also, did I miss something?
Jun 9, 2009 at 5:03pm
no your right, i wrote that 2 mins outta bed so i dont know what i was thinking lol
 
while(num<1||num>5);
Last edited on Jun 9, 2009 at 5:04pm
Jun 25, 2009 at 10:55pm
Sorry, what I meant to say was, if the user pressed r, the program restarts.
Jun 25, 2009 at 11:54pm
is it really that hard to figure out, we just told you how to do it with an int,
just do it with a char
1
2
3
4
5
char again;
do{
// all your code
cin>>again;
}while(toupper(again) == 'R');
Jun 26, 2009 at 1:34am
Thanks.
Last edited on Jun 26, 2009 at 1:38am
Topic archived. No new replies allowed.