Hi! I need help! I need to create a program in c++ that ask a question, there is a multiple choice, display if your answer is wrong or correct. Like the gameshow "Who wants to be a millionaire" and included "LIFELINE"

I tried and it runs but when I chose "LIFELINE # 2" its also display the lifeline #1 and #3. and sometimes when i chose lifeline #1, it suddenly closed.

I'm so sorry i'm not really good about this. :'(
and sorry for my english. lol.
this is what i have done as of now >>:

#include<iostream.h>
#include<conio.h>

char ans;
char response;
int life;
int main()
{
clrscr();
cout<<"Who Wants to be a Millionaire?";
cout<<"Folder contains:?";
cout<<"a. Files";
cout<<"b. Foods";
cout<<"c. Animals";
cout<<"d. Stools";
getch();
cout<<"Do you want to use lifeline?  Y/N: ";
cin>>response;
if(response=='y')
cout<<"1. 50-50";
cout<<"2. Call a friend";
cout<<"3. Ask the audience";
cout<<"Choose Lifeline [1-3]: ";
cin>>life;
{
if(life==1)
cout<<"50-50";
cout<<"a. Files";
cout<<"c. Animals";
{
if(life==2)
cout<<"Call a friend";
cout<<"Hi friend! I think the answer is letter B.";
cout<<"Im 89% sure. Good Luck!";
{
if(life==3)
cout<<"Ask the audience";
cout<<"The audience answers:";
cout<<"(a.) 52%   (b.) 23%   (c.) 13%   (d.) 12%";

getch();

{ else if(response=='n')
cout<<"Your answer is: ";
cin>>ans;
switch(ans)
{
case 'a':
cout<<"a. Files";
getch();
cout<<"Correct!";
break;
case 'b':
cout<<"b. Foods";
getch();
cout<<"Wrong!";
break;
case 'c':
cout<<"c. Animals";
getch();
cout<<"Wrong!";
break;
case 'd':
cout<<"d. Stools";
getch();
cout<<"Wrong!";
break;
default:
cout<<"Invalid Choice!";
break;
}
getch();
return 0;
}}}}}}

Recommended Answers

All 7 Replies

"There is an error" or "it does not work" are not very accurate descriptions of a problem. Try to explain the problem so people can help you.

You seem friendly. God I want to help you. You have an error, I must help.
The only problem is.... I don't know what your problem is.
- Compiler error? Post it here.
- Runtime expectation mismatch? What happens, and what doesn't?

Almost a good and useable post that could get answers, would just make life easier for us to do so, if we knew a bit more about the mentioned error you have.

Hey, there.. I love your code. Why not use switch() for your lifelines?

I am assume that the code that you have provided was a retype as it would not run. using namespace std; is the minor issue the bigger concern is that your if blocks are out of sync.

I would recommend that you always nest your if blocks with {} this protects you from making mistakes that are difficult to read

if(life==1)
cout<<"50-50"; 
cout<<"a. Files";
cout<<"c. Animals";
{

This if condition wants to run for just life == 1 so

if(1 == life)
{
cout << "50-50";
cout<<"a. Files";
cout<<"c. Animals";
}
else if(2 == life)
{
}

/*
//equivalent to what you wrote is
if(1 == life)
{
cout << "50-50";
}
//this will always run
cout<<"a. Files";
cout<<"c. Animals";
*/

You would also benefit a lot from having a question class
where you would need to track the
answer option_id
store 5 strings:
question
option_a
option_b
option_c
option_d

then you can add in your other methods

hi! thank you soooo much! it works!! luvyah!!!!!!!!!

I am assume that the code that you have provided was a retype as it would not run. using namespace std; is the minor issue the bigger concern is that your if blocks are out of sync.

I would recommend that you always nest your if blocks with {} this protects you from making mistakes that are difficult to read

This if condition wants to run for just life == 1 so

if(1 == life)
{
cout << "50-50";
cout<<"a. Files";
cout<<"c. Animals";
}
else if(2 == life)
{
}

/*
//equivalent to what you wrote is
if(1 == life)
{
cout << "50-50";
}
//this will always run
cout<<"a. Files";
cout<<"c. Animals";
*/

You would also benefit a lot from having a question class
where you would need to track the
answer option_id
store 5 strings:
question
option_a
option_b
option_c
option_d

then you can add in your other methods

----------------------------------------------------------------------------------

Thank you!! it works!! but I still have one more question,. lol
when I chose yes for lifeline and it display the lifeline number but when I answerd the "Your answer is: " it suddenly closed..

this is the code:

#include<iostream.h>
#include<conio.h>
char ans;
char ask;
int life;
int main(void)
{
clrscr();
box(5,50,1,45);
box(55,78,8,30);
gotoxy(62,12);cout<<"LIFELINE";
gotoxy(57,15);cout<<"1. 50:50";
gotoxy(57,18);cout<<"2. Call a friend";
gotoxy(57,21);cout<<"3. Ask the audience";

gotoxy(13,3);cout<<"Who Wants to be a Millionaire?";
gotoxy(10,5);cout<<"The Folder contains?";
gotoxy(10,9);cout<<"a. Foods";
gotoxy(24,9);cout<<"b. Files";
gotoxy(10,11);cout<<"c. Animals";
gotoxy(24,11);cout<<"d. Clothes";
getch();
gotoxy(9,13);cout<<"Do you want to use your lifeline?";
gotoxy(13,14);cout<<" Y/N : ";
cin>>ask;
if(ask=='y')
{
gotoxy(10,15);cout<<"Choose Lifeline Number: ";
cin>>life;
switch(life)
{
case 1:
gotoxy(18,17);cout<<"[ 50:50 ]";
gotoxy(10,19);cout<<"_________";
gotoxy(24,19);cout<<"c. Animals";
gotoxy(10,21);cout<<"b. Files";
gotoxy(24,21);cout<<"_________";
break;
case 2:
gotoxy(18,17);cout<<"[ Call a friend ]";
gotoxy(10,20);cout<<"Friend: I think it is B";
getch();
gotoxy(10,23);cout<<"You: How sure are you?";
getch();
gotoxy(10,26);cout<<"Friend: I'm 95% sure about this.";
break;
case 3:
gotoxy(10,16);cout<<"Ask the audience";
break;
default:
gotoxy(10,17);cout<<"Invalid Choice!"<<endl;
break;
}
getch();
}
else if(ask=='n')
{
gotoxy(10,30);cout<<"Your answer is: ";
cin>>ans;
switch(ans)
{
case 'a':
gotoxy(10,32);cout<<"a. lol";
gotoxy(10,35);cout<<"Wrong!";
break;
case 'b':
gotoxy(10,32);cout<<"b. toinkz";
gotoxy(10,35);cout<<"Correct!";
break;
case 'c':
gotoxy(10,32);cout<<"c. lmao";
gotoxy(10,35);cout<<"Wrong!";
break;
case 'd':
gotoxy(10,32);cout<<"d. wenkz";
gotoxy(10,35);cout<<"Wrong!";
break;
default:
gotoxy(10,38);cout<<"Invalid Entry!";
break;
}
getch();
return 0;
}}

is there some code that i missed? I cant find what is the missing code. :'(

I suspect what is happening is that your code is running too fast for you to see it then closes the window.

This will be down to how you are running the application

you can either run the .exe directly from dos or command shell in which case the window stays open or I think the command that is used is

system("pause");
return 0;

but I have never used it and it is a bad programming habit
there are other ways to delay the system

if you are using visual studio use ctrl + F5 to run your code instead of F5

otherwise for now you can add a cin>>var the reason for doing this is eventually you would want a more
robust system where you would be in a while loop.

int main(void)

don't use void as an argument

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.