0

My problem is that I'm trying to make a menu as part of my program. There are conditional statements for input, and my else statement is supposed to say that input is invalid, and wait for input (so as to let the user know they entered something invalid).

This is the relevant snippet, and what I tried:

void main_menu() {
    int opt;
    system("CLS");
    std::cout << "Main Menu" << std::endl;
    std::cout << "\n\nWhat would you like to do?" << std::endl;
    std::cout << "1) Fight" << std::endl;
    std::cout << "2) Store" << std::endl;
    std::cout << "3) Exit" << std::endl;
    std::cin >> opt;
    std::cin.ignore();

    if (opt == 1) {
        return main_menu();
    }
    else if (opt == 2) {
        return main_menu();
    }
    else if (opt == 3) {
        return;
    }
    else {
        system("CLS");
        std::cout << "Invalid!" << std::endl;
        std::cin.get(); //error, but not seen in list or console
    }
}
int main() {
    main_menu();
    return 0;
}

What I seem to have screwed up on is the else. When I run it, the code seemingly passes the std::cin.get() I've set up.

My desired output:

Input = 1 // valid input
go to statement 1 (empty for time being.)

Input = 6 // invalid, but is handled with the else:
cout << "Invalid!" << ....
//pause

Input = 'a' //invalid, should be like above:
cout << "Invalid!" << ....
//pause // in reality, it passes

I have already tried some methods and looked at some posts in sites other than SO (see post Why is the Console Closing after I've included cin.get()?) (I have tried the cin.ignore() after cin >> var method already, not working), but none work.

It does say about newlines and such, but I don't understand about it. Can someone explain how the trailing newlines work, and why is my snippet not working?

EDIT: Other statements of cin.get() have worked in my actual code.

EDIT: I tried inputting an invalid option other than a number, but it didn't work (say, chars). Nums work though. So the question is now about handling data types other than ints, floats, doubles, etc.

S.G. Harmonia
  • 297
  • 2
  • 18
  • `opt` is `int` type, isn't it? – BiagioF Jul 28 '17 at 01:52
  • Yes, I have read from the linked post that it might be a trailing newline from the int input. – S.G. Harmonia Jul 28 '17 at 01:53
  • 1
    Do `std::cout << (int)std::cin.get();` , then you can see what character was read. You might have better luck if you ignore the rest of the line, instead of just one character, earlier on – M.M Jul 28 '17 at 02:07
  • Also it would be good specify the exact input you provide to produce the unexpected behaviour. And post a [MCVE](http://stackoverflow.com/help/mcve) – M.M Jul 28 '17 at 02:07
  • Your example works for me, however I did remove the `system` call. – Retired Ninja Jul 28 '17 at 02:13
  • How did you manage to compile code that does `return;` in a value-returning function? – AnT stands with Russia Jul 28 '17 at 02:23
  • 1
    You are making something up. Your `std::cin.get();` works as expected. So, it is not clear what "error" are you talking about. What made you think there was an "error" of any kind? "Not working" is not a meaningful description of the problem. – AnT stands with Russia Jul 28 '17 at 02:25
  • Sorry, was using `main()` as a holder for the recursive function's actual name. – S.G. Harmonia Jul 28 '17 at 02:25
  • @AnT I edited that it had to do with chars, sorry for the confusion. – S.G. Harmonia Jul 28 '17 at 02:25
  • I took the returns out since I did not have the other functions. If an actual complete example existed it would make testing and debugging much easier. – Retired Ninja Jul 28 '17 at 02:26
  • you haven't stated your expected behavior. It appears it would wait for an ENTER key and then exit the program. – Garr Godfrey Jul 28 '17 at 02:31
  • 1
    Finally you post enough code and what you want. Inputting a char leaves the stream in an error state because it is expecting an int so `get` fails, you need `cin.clear()` to fix that. – Retired Ninja Jul 28 '17 at 02:42

1 Answers1

0

by default, ingore will only ignore 1 character. There are at least 2 characters after the input in windows \r and \n.

You would be better to do:

cin.ignore(999,'\n')
Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23