1

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?

drpogue
  • 133
  • 1
  • 4
  • 12
  • OK so what exactly is the issue here. The menu never shows up, or is it when the menu does show and you input a selection it does not do the right thing? – Nomad101 May 02 '13 at 01:15
  • The menu shows up, but then it never takes the input as correct. It just returns "CLS : INVALID" – drpogue May 02 '13 at 01:15
  • 1
    hmmm well firstly take a look at this to know why to never use system(). Or at least why you should be very careful when you do: http://www.cplusplus.com/articles/j3wTURfi/ Also take a look at this for alternatives: http://www.cplusplus.com/forum/articles/10515/ It does seem from the error message that the system() call is what the issue is. Have you stepped through it with a debugger. To see which case is being selected? – Nomad101 May 02 '13 at 01:17
  • What is the value of x when the switch always jumps to the default branch? That may shed some light. For example if you key in number on the numeric keypad, depending on the state of num lock you may first get an indicator of special character and then you;ll get the real character. – Csaba Toth May 02 '13 at 02:22

1 Answers1

1

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?

Community
  • 1
  • 1
Skurmedel
  • 21,515
  • 5
  • 53
  • 66