Help Making Who Wants to be a Millionaire

Still new here, need help trying to figure this out. its for my school project due in a few hours. Still having a hard time with loops...i need to be able to ask 10 questions with 3 life lines...i cant even get past the first question...smh. just a nudge in the right direction will do. thanks.

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
#include <iostream>

using namespace std;

int lifeline = 3, points, ans;

int main()
{
cout << "Question 1: Who was the first president of the United States of America?" << endl;
	if (lifeline < 0)
	{
		cout << "Would you like to use a lifeline? You have: " << lifeline << " remaining. Yes(y) or No(n)" << endl;
		cin >> ans;
			if (ans == 'y' || ans == 'Y')
			{
				lifeline--;
				cout << "George Washington(1) Herbert Hoover(2)" << endl;
				cin >> ans;
				if (ans = 1)
				{
					points ++;
					cout << "You are correct!" << endl;
				}
			}
				else
					cout << "Sorry the correct answer was George Washington! Better luck next time!" << endl;
					return 0;
			{
				cout << "George Washington(1) Abraham Lincoln(2) Barack Obama(3) Herbert Hoover(4)" << endl;
				cin >> ans;
				if (ans = 1)
				{
					points ++;
					cout << "You are correct!" << endl;
				}
			}
				else
				{
					cout << "Sorry the correct answer was George Washington! Better luck next time!" << endl;
					return 0;
				}

return 0;
}
Generally, if you need help, it's a good idea to ask for help sooner than a few hours before the deadline.
There're some issues with your code sample:

using using namespace std; in the global scope is considered amature and bad practice, as it tends to introduce name clashes. Consider either localizing the using directive in function scope or removing it all-together and simply clarifying the scope resolution when you need it.

On lines 19 and 31, you've written the following:
if (ans = 1)
Which doesn't do what you think it does. Probably not intentional as you've correctly used the binary equality operator on line 14.

Additionally, your code isn't very expandable, and painfully hard-coded. One possible solution is to have a game class which holds a container of question objects. The question object ctor might read questions and false and correct answers from a dedicated file. You could even shuffle the container to randomize the sequence in which the questions are asked.
Last edited on
okay here is where i am now after changing my code around a little. im trying to get it to open q1.txt to read the question and answers from my text file onto the screen. but when it gets to the like it freezes there and doesnt display anything.

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
void help() //help function
{
	cout << "You will guess the answers to the questions shown on screen and recieve points. Miss one question and game over." << endl;
	cout << "Good luck!" << endl;
	cout << "" << endl;
	cout << "" << endl;
	cout << "" << endl;
}


void play() //play function
{
	ifstream inputFile;
	string question;

	inputFile.open("q1.txt");
	inputFile >> question;
	cout << question;
}

int main()
{

int choice;
	
	begin: // restart 
	cout << "WHO WANTS TO BE A MILLIONAIRE!" << endl;
	cout << "Select from the following: Play Game(1) Help(2) Exit(3)" << endl;
	cout << "" << endl;
	cout << "" << endl;
	cin >> choice;


	if (choice == 1) //PLAY
	{
		play(); //function
		goto begin;
	}

	else if (choice == 2) //HELP
	{
		help(); //help function
		goto begin;
	}

	else if (choice == 3) //EXIT
	{
		exit(0);
	}

	else
	{
		cout << "Please enter a valid choice.";
		goto begin;
	}

}



im not getting any errors, what am i doing wrong?


edit: by replacing with getline i was able to correct my problem
Last edited on
Topic archived. No new replies allowed.