Hello again,
I believe I understand what you are looking to do. To further clarify, are you looking to re-ask the question of continuing if not 0 or 1 [invalid] by the users input?
If so there are 2 main ways to accomplish this:
- Use the goto statement
- Split your program into different functions
The second is most recommended, though I will explain both.
Using goto
Explaining the
goto statement first, lets look at our modifications:
Above the line:
cout<<"\n1 to continue and 0 to exit\n";
Lets put:
ask:
cout<<"\n1 to continue and 0 to exit\n";
Simple. C provides the infinitely-abusable
goto statement. The
goto statement labels to branch to.
ask here is our scope of a label in the entire function.
Also, instead of setting
ContExit = 0; in your
else to find
x, lets set it to
x so we know what was entered. Even if 0 was entered, we would still know about it. Ex:
After this, in your switch statement include the
default case. Why? The case labeled
default is executed if none of the other cases are satisfied. Exactly what we need! In the case, we tell the compiler to "
goto ask;"
Here is a look:
switch (ContExit) {
case 0:
...
case 1:
...
default:
goto ask;
}
Now when you compile, if the user did not type 0 or 1, it will go back and ask them what they want infinitely until they choose a valid choice.
Splitting your code
This way is more often reliable, and less dependant. This way, we can split sections of your code in seperate functions. Have a look:
#include <iostream.h>
// Put calls up here so compiler doesn't whine
void getinput(int *, char *);
// Make this one return int, so return 0 doesn't conflict
int makechoice(int, char *);
// Your own function seperate from main() for easy call
void getinput(int *choice, char *name) {
int x;
cout<<"\n1 to continue and 0 to exit\n";
cin>>x;
if (x == 1)
*choice = 1;
else
*choice = x;
makechoice(*choice, name);
}
// Here is the decision making factor
int makechoice(int choice, char *name) {
switch (choice) {
case 0: cout<<"\nCya Next time!";
return 0;
case 1: cout<<"\nYou are faced with one exit:\n1: North\n";
int y;
cin>>y;
if (y == 1) {
cout<<"\nThank you for playing the game"<<name<<"!\n"<<"Hope to see you next time!\n";
return 0;
}else {
cout<<"\nYou didnt enter a valid Direction!\n";
}
default:
// call again in case we dont get the results we want
getinput(&choice, name);
}
return 1;
}
int main() {
// Our local variables
int ContExit;
char UsrName[10];
cout<<"Welcome to Dungeon!\nPlease enter your name!\nYour name can only be up to 10 characters long.\n";
cin >> UsrName;
if (strlen(UsrName) > 10) {
do {
cout<<"You have entered a name too long!\n";
cin>>UsrName;
} while (strlen(UsrName) > 10);
}
cout<<"Welcome "<<UsrName<<"!";
cout<<"\nYou are about to embark on the shortest god damn maze of your life..\nstraight forward is the exit.\nThere is no left nor right just yet!\nHave fun "<<UsrName<<"!\n";
// Call our function here
getinput(&ContExit, UsrName);
return 0;
}
I commented our new areas. Most is self-explanatory. Though, lets take a quick look on how this works:
Review
We make two functions outside of main() called getinput() and makechoice().
Next we pull out the input factor and move it into getinput(). getinput() takes two parameters, your choice, and name. Even though the choice hasn't been given yet, we will send the memory address of
ContExit to getinput(). This will allow us to change the variable state somewhere else, and if you come back to main() and check
ContExit's value, it will be what getinput() set it to last.
After that, we call on makechoice(). makechoice() also takes two parameters, choice and name. This time, we don't send the memory address of our choice to this function because we already know the answer. makechoice() determines what the outcome of the program will do next. 0 exits, 1 moves on, invalid re-asks the question using the
default label. We send the memory address of choice in makechoice() back to getinput() for re-evaluation if an invalid choice was received. We also need some way to track your name, so we pass it around all of the functions.
Summary
I do hope this has shed some light on the issue, and can now help you understand more of the C language.
If you have any questions, please feel free to ask. I will try to help you further in any way possible to clearly understand the syntax above.
-
Stack
Overflow