CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2011
    Posts
    0

    Video-game Progamming project [help]

    Hello codeGuru,
    This is my first post, so I figure I would start by saying Hello. I'm a student at Auburn University in a C++ class, and I've come here to get some advice as I am new to the C language. I'm almost done with my project, which is a text based video game. I currently have 4 classes to run this program. Player, Hall, Menu and Engine. The class Menu is a class to display the options available in the menu based game, and return the value entered by the user. Currently, if you enter a value that is not a integer it will set the value of menuChoice to a char or string and then menuChoice can't be reset to an int. I can't figure out why my function isn't looping until the input I enter is valid. Here is a snippit of my code so you can get an idea of whats going on:

    bool Menu::isValidInput(int input)
    {
    return input > 0 && input < 6;
    }


    int Menu::gameMenu()
    {
    int menuChoice = 0;

    cout << "1) Move (takes time, could be risky...) " << endl;
    cout << "2) Read technical papers (boost intelligence, takes time) " << endl;
    cout << "3) Search for loose change (boost money, takes time) " << endl;
    cout << "4) View Character " << endl;
    cout << "5) Quit the game " << endl;
    cout << "\nPlease choose an action: ";
    do
    {
    cin >> menuChoice;
    }
    while(isValidInput(menuChoice) == false);
    return menuChoice;
    }



    I've tried using cin.clear(); cin.sync(); along with various other cin methods and nothing works. I think it's because menuChoice is declared an int and if the user inputs a char like 'z' whenever menuChoice is set to a char it can't be reset to a int??

  2. #2
    Join Date
    Dec 2010
    Posts
    20

    Talking Re: Video-game Progamming project [help]

    add "cin.ignore(); " after "cin.clear();"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured