Reputation: 133
So I've got a game that when I run it, begins by calling the function menu()
. For some reason it won't take the input and correctly go the to next step. Any ideas?
void menu() {
char x = -1;
while (x != '3') {
cout << "\n";
cout << "\n";
cout << "\n---------------------";
cout << "\n Main Menu ";
cout << "\n 1 - Start a New Game";
cout << "\n 2 - Instructions ";
cout << "\n 3 - Exit Game ";
cout << "\n---------------------";
cout << "\n";
cout << "\n";
cout << "\n Enter Selection:";
cin >> x;
switch (x) {
case '1':
for(i=0; i<15;i++){
cout<<"\n"
}
playgame();
break;
case '2':
for(i=0; i<15;i++){
cout<<"\n"
}
instructions();
break;
case '3':
for(i=0; i<15;i++){
cout<<"\n"
}
cout << "Good bye\n" << endl;
break;
default:
cout << "Invalid Character\n";
cout << "\n";
cout << "Press Space to continue\n";
}
for(i=0; i<15;i++){
cout<<"\n"
}
}
}
I changed it up so it just clears the screen by using a for loop and "\n". But now it doesn't hit the next line for some reason.
EDIT, now my menu() isn't working. It's asking for the input, then hitting the for loop to clear, then never does the next line. Am I passing the input incorrectly? Or something else?
Upvotes: 1
Views: 364
Reputation: 22159
If you are on Ubuntu/Debian (or any kind of Linux really) system("CLS")
will probably not work. CLS
is the DOS-command to clear the terminal.
If the system uses sh
etc. you can use clear
to the same effect. But you should probably avoid system
all together and look in to more robust alternatives.
I use bash on Windows and the results are even more hilarious on this configuration, as system
uses cmd.exe
on Windows and thus CLS
works, but not clear
.
It throws a hissy fit if the following program is executed:
#include <stdlib.h>
int main(int argc, char *argv[])
{
system("clear");
}
As you can see what exactly happens when you run system
is wholly dependant on the underlying shell. And the underlying shell might not be the shell you execute your program from. Which can be quite scary.
This question has some nice, related, answers:
How do you clear console screen in C?
Upvotes: 1