Program end problem

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;
}
Hello Mariyan,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

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.

Hope that helps,

Andy
Thanks for the help. It worked. And I will try to follow what you told me about the code!
Hello Mariyan,

You are welcome. Glad it worked out for you.

While working with yur code I came up with this. You may fine it worth something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#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.
Last edited on
Topic archived. No new replies allowed.