Hello everyone.So i am making a quiz program which you'll see below.My question is how to make the program end if the answer of the question is false.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string answer1;
string answer2;
cout <<"Hello.This is a game quiz with.Hope you like it.Let's get started!"<< endl;
cout <<"Tell me when did Minecraft first came out"<<endl;
cin>>answer1;
if (answer1=="2009")
cout<<"CORRECT"<<endl;
else
cout<<"INCORRECT"<<endl;
cout<<"Next question!When did League of Legends first came out?"<<endl;
cin>>answer2;
if (answer2=="2009")
cout<<"CORRECT.Again.League of Legend was first realeased the same year as Minecraft"<<endl;
else
cout<<"INCORRECT"<<endl;
return 0;
}
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
One possibility is:
1 2 3 4 5 6
else
{
cout<<"INCORRECT"<<endl;
return 1;
}
The 1 of the return would let you know that you ended the program because of an incorrect answer.
You only really need one std::string answer; since this can be reused. unless you have some future idea and need both strings.
cin>>answer1; will work for a single word answer, but should an answer contain a space I would suggest using "std::getline()".
The program appears simple enough, but all these changes are untested for now.
#include <iostream>
#include <string>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/int main()
{
size_t index{}, correctAnswers{};
std::string questions[]
{
"\n Tell me when did Minecraft first came out? ",
"\n Next question!When did League of Legends first came out? "
};
std::string answers[]
{
"2009",
"2009"
};
std::string answer;
std::cout << "\n Hello.This is a game quiz with.Hope you like it.Let's get started!\n";
std::cout << questions[index];
std::getline(std::cin, answer);
if (answer == answers[index++])
{
std::cout << "\n CORRECT" << std::endl;
correctAnswers++;
}
else
{
std::cout << "\n INCORRECT" << std::endl;
//return 1;
}
answer.clear(); // <--- Added. Clears the string. This line is optional.
std::cout << questions[index];
std::getline(std::cin, answer);
if (answer == answers[index])
{
std::cout << "\n CORRECT. Again. League of Legend was first realeased the same year as Minecraft" << std::endl;
correctAnswers++;
}
else
{
std::cout << "\n INCORRECT" << std::endl;
//return 1;
}
std::cout << "\n\n You got " << correctAnswers << " answer" << (correctAnswers > 1 ? "s " : " ")
<< "correct." << std::endl;
return 0;
}
If you have learned about "vectors" you can replace the arrays with "vectors". You could also add a third array for responses, since the second question has a different response. You could even create a function to print the question, check the answer and give the correct response.
Have a look and see what you think.
Hope that helps,
Andy
P.S. A few blank lines makes the code easier to read.