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