0

I know this may seem like a simple problem, but I have an assignment for class to make a 20 Questions type game. I thought I'd do a bit extra and make a menu. The program runs fine, but when I enter the actual game file, It outputs the first line then goes to my last else and outputs that... It won't let me type and play the game. Is this easy to fix?

When I start my program, the menu runs as it should. It prompts for user input to navigate (1=play game, 2=instructions, 3=word database, 4=exit). When I press 1 to pay the game, it clears the screen like it should but then instead of letting my type my answer to the first question (animal, food, element, other), it says "Is it animal, food, element, or other? (Enter a, f, e, or o to make a selection)", just like it should, but then takes my inputted 1 and uses it as input for this question, making it output "That's not an option!" before prompting for input to close the program. How can I fix this? I'm not keen on including all of my code as it is a few hundred lines long, but here's my menu file, header file, and beginning of my game file:

myheader.h:

#include <windows.h>
#include <iostream>

void runGame();
void showHowTo();
void showWords();
void mainMenu();

menu.cpp:

#include <iostream>
#include <string>
#include "myheader.h"
#include <windows.h>

using namespace std;

int main()
{

    int x = 0;
    cout << "*******************************\n";
    cout << "*****20 QUESTIONS**************\n";
    cout << "*******************************\n\n";

    cout << "Press a key to make a selection.\n\n\n";

    cout << "1: Play Game" << endl;
    cout << "2: View Instructions" << endl;
    cout << "3: View Word Database" << endl;
    cout << "4: Close Window\n\n\n";

    cin >> x;

    if (x == 1)
    {
        system("cls");
        runGame();
    }
    else if (x == 2)
    {
        //instructions function would be here, but was removed as it is not relevant to my question
    }
    else if (x == 3)
    {
        //word database function would be here, but was removed as it is not relevant to my question
    }
    else if (x == 4)
    {
        system("pause");
        return 0;
    }



    system("pause");
    return 0;
}

game.cpp:

#include <iostream>
#include <string>
#include "myheader.h"
#include <windows.h>

using namespace std;

void runGame()
{
    //declaring variables

    string userInput = "";
    bool no = ((userInput == "no") || (userInput == "No") || (userInput == "NO") || (userInput == "nO"));
    string disappointment = "Awww... Oh well, maybe I'll get it next time!\n";
    string excitement = "Yay! I feel smart!\n";
    bool animal = ((userInput == "A") || (userInput == "a"));
    bool food = (userInput == "F" || userInput == "f");
    bool element = (userInput == "E" || userInput == "e");
    bool other = (userInput == "O" || userInput == "o");

    // Prompt user for input (animal, vitamin, element, or other

    cout << "Welcome to 20 Questions!\nPlease think of something, and answer the questions honestly!\n\n";
    cout << "Is your object an animal, vitamin, element, or other? (Please answer A, F, E, or O)\n";
    getline(cin,userInput);
    animal = ((userInput == "A") || (userInput == "a"));
    food = (userInput == "F" || userInput == "f");
    element = (userInput == "E" || userInput == "e");
    other = (userInput == "O" || userInput == "o");

    if (animal)
    {
        //full game in here
    }
    else
    {
        cout << "That's not an option!\n";
    }

    system("pause");
}

Those are the code files I'm struggling with... Please don't point out that system("pause") isn't efficient, I already know that. I just need to know how to get my user input working when the game begins to run. These are photos of my output if my description was horrible.

Menu:

enter image description here

Game

enter image description here

Please help! I need this to work for tomorrow, and I'm stuck!

EDIT: FIXED THANKS TO @ThomasMatthews!!! I fixed it by putting getline(cin,userInput); twice one on top of the other. Not sure why it worked, but it did! My program runs fine now! Thank you!!!!

eyllanesc
  • 190,383
  • 15
  • 87
  • 142
  • Your output images are broken links for me. – JGroven Apr 06 '17 at 22:55
  • @JGroven right click and open in new tab. Not sure why they didn't work :/ here they are if you can't get it: – Freak Demon Apr 06 '17 at 22:56
  • @JGroven [menu](https://ibb.co/g9uttv) [game](https://ibb.co/huQNmF) – Freak Demon Apr 06 '17 at 22:57
  • 1
    Reduce your compares by half by using `std::tolower` or `std::toupper` before comparing. – Thomas Matthews Apr 06 '17 at 23:00
  • how about adding `cout << userInput` just after the getline so you can see what you think you are getting. – pm100 Apr 06 '17 at 23:01
  • @ThomasMatthews what do you mean?? – Freak Demon Apr 06 '17 at 23:02
  • @pm100 I just tried that. Nothing was outputted... – Freak Demon Apr 06 '17 at 23:03
  • By converting to all lower case or all upper case, you only need to make one comparision: `char c = std::toupper(userInput[0]); bool animal = (c == 'A');` – Thomas Matthews Apr 06 '17 at 23:04
  • In `rungame`, you are comparing an empty string (first two statements). The `no` variable should be false because none of the conditions will be met. – Thomas Matthews Apr 06 '17 at 23:06
  • @ThomasMatthews fixed thanks to your comment. I put two getline statements on consecutive lines, and that worked. and yeah I get what you mean, but we're supposed to be using just what we learned in class. Using mutiple files is way farther than I was supposed to do :/ I'm just looking at your toupper and tolower comments: how would those help? It seems to run fine now.... Is it just something I should make a habit instead of declaring large booleans? – Freak Demon Apr 06 '17 at 23:15
  • Good habit to get into. More code == more places to put bugs. – user4581301 Apr 06 '17 at 23:17
  • Possible duplicate of [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Miles Budnek Apr 06 '17 at 23:25
  • @user4581301 okay thanks! I'lll start using it more often – Freak Demon Apr 06 '17 at 23:33
  • @MilesBudnek This is quite different, especially after I saw that post and still could not solve before... – Freak Demon Apr 06 '17 at 23:34
  • @MilesBudnek and using getline twice was faster... – Freak Demon Apr 06 '17 at 23:35
  • This is exactly the same as the dup I linked. You call `getline` after `cin >> x`, so there's a `\n` character still in the input stream for `getline` to see. – Miles Budnek Apr 06 '17 at 23:36

1 Answers1

1

Because you do cin >> x you STDIN actually has an extra character in the buffer (the new-line when you press enter)

Therefore your getline(...) is actually getting the enter key that is already in the buffer.

You can fix this by doing a cin.ignore(); beforehand, or by using getline(...) instead of cin >> x

Eyal Cinamon
  • 939
  • 5
  • 15