char question and another problem

hello everyone, i have a question about chars and ill post the code also i have another question.

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
62
63
64
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

int main()
{
	srand (time(NULL));
	int grade, operands[2], userAnswer;
	string quiz[20], name;
	char answer[12], userchoice( '\0' );

	cout << "What is your name?\n";
	cin >> name;
	cout << "Were going to take a quiz, are you ready? <y/n>\n";
	cin >> answer[0];
	
	if(answer[0] == 'y' || answer[0] == 'Y')
	{
	cout << "Ok lets get started " << name << endl;
		cout << "First lets do a math problem.\n";
		operands[0] = rand()%50;
		operands[1] = rand()%20;
		/*******************first question********************************/
		
	do
	{
		cout << "What is " << operands[0] << " + " << operands[1] << " ?\n";
		cin >> userAnswer;
		
	if(userAnswer==operands[0] + operands[1])
	{
		cout << "You got the Problem right, off to the next question\nYou ready?\n";
		cin >> answer[1];
	}
		else
		{
		cout << "Problem incorrect\n";
		cout << "Would you like to try again?\n";
		cin >> userchoice;
		}
		}while(userchoice == 'y' || userchoice == 'Y');

	/*********SECOND?***************/
	if(answer[1] == 'y' || answer[1] == 'Y')
	{
		do
		{
		cout << "Who was the president of the united states in 1865?\n";
		if(quiz[19]=="Abraham Lincoln" || quiz[19] == "abraham lincoln")
			cout << "Good job, off to the next question\n";
		else
		{
			cout << "Problem incorrect\n";
			cout << "Would you like to try again?\n";
			cin >> userchoice;
		}
		
		}while(userchoice == 'y' || userchoice == 'Y');
	}
	}
	system("pause");
	return 0;
}


is this a correct way to use the char and when i run this program, after you get the first question right it ask are you ready for the next question you say 'y' and it takes you to the next question but it automaticly tells you that its wrong and skips to the system("pause") any help appriciated thank you
closed account (3qX21hU5)
The reason why it is skipping to the system("pause") is because on line 49 you ask the question
cout << "Who was the president of the united states in 1865?\n";
But you never let the user answer the question you just go straight into check if (quiz[19] == "Abraham Lincoln"). So you need to add in a cin >> quiz.

Also I would recommend you switch to using strings since they are much easier to manage and you are using C++ ;p
thank you sir, i didint even relize that haha, i also just changed this part to a multiple choice one, i also have a nother question if i could, when i answer the first question wrong, then do the question again and get it right when i say yes to going to the next question, it just repeats the first question. any ideas on that?
closed account (3qX21hU5)
Because you when get the answer wrong the first time you set userchoice to equal 'y' or 'Y'. So then you do the first question again and when you get it right it will keep on looping through the first question over again over since userchoice is == 'y' from the first time you did the question.

To fix this you will want to make userchoice = ' ' equal to a blank char at the beginning of the do while loop.

Hope that makes sense getting kinda late here and my mind is wandering lol.
Last edited on
thank you ill try it, im still learning C++ and im just learning from the internet and i dont understand things the first time around. haha thanks thou,
closed account (3qX21hU5)
No problem everyone one of us has been in the same position as you. Just keep with it and in a few months it will start to make a lot more sense.
Topic archived. No new replies allowed.