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:
Game
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!!!!