Recently I've tried to challenge myself by trying to make a quiz game, however, as expected I've stumbled upon a error already and I can't resolve it. Well, there's no errors, but it's not working as intended, any suggestions on what to modify ?
Also, I'm a very beginner to C++ (still learning), so this might seem very dumb to others since I can already assume the problem is trivial.
Here's the code:
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <unistd.h>
bool strQuiz = false;
void welcomeScreen(void)
{
std::cout << "Welcome to Quizlet" << std::endl;
std::cout << "We'll ask you a bunch of questions" << std::endl;
std::cout << "When you're ready please type \"ready\" or \"exit\" to leave" << std::endl;
};
int startQuiz()
{
int wait = 10;
std::string userInput;
while (!strQuiz)
{
getline(std::cin, userInput);
if(userInput.compare("exit") == 0 && userInput.compare("") == 0) strQuiz = false;
if(userInput.compare("ready") == 0)
{
std::cout << "Quiz starting in a few seconds...";
sleep(wait);
}
}
std::cout << "You've ended the Quiz";
system("PAUSE");
return EXIT_SUCCESS;
}
int main()
{
welcomeScreen();
startQuiz();
}
The issue is that although there are no errors, when I write ready or exit in terminal and hit enter nothing happens, it's just stuck at input. Issue is in startQuiz() function since welcomeScreen() function runs perfectly.
The loop I attempted in this project was from a previous project that I've done; Project: 8-Ball Game
FYI, I've attempted to change the function type since it was previously void but changed to int so that it can return termination status.
Edit:
Tried changing up the code, no change :(
int startQuiz()
{
int wait = 100;
std::string userInput;
while (!strQuiz)
{
getline(std::cin, userInput);
if(userInput.compare("exit") == 0) strQuiz = false;
// Change
else if(userInput.compare("") == 0)
{
userInput = "";
std::cout << "Invalid Input, please try again" << std::endl;
}
if(userInput.compare("ready") == 0 && userInput.compare("exit") != 0)
{
std::cout << "Quiz starting in a few seconds...";
sleep(wait);
}
// Change
}
std::cout << "You've ended the Quiz";
return EXIT_SUCCESS;
}
Edit#2:
I apologize for wasting your time, I've realized the issue, I should've brainstormed more but I was honestly stuck. But your comments were very helpful, thank you.
Aucun commentaire:
Enregistrer un commentaire