Hi! I need help for my c++.

This is the code:

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

void box(int x, int xx, int y, int yy);
void box(int x, int xx, int y, int yy)
{
int xs,ys,i,j;
xs=xx-x;
ys=yy-y;
for(i=1; i<xs; i++){gotoxy(x+i,y);cout<<"Í";
		    gotoxy(x+i,yy);cout<<"Í"; }
for(j=1; j<ys; j++){gotoxy(x,y+j);cout<<"º"; }
for(j=1; j<ys; j++){gotoxy(xx,y+j);cout<<"º"; }
		   {gotoxy(x,y);cout<<"É";
		    gotoxy(x,yy);cout<<"È";
		    gotoxy(xx,y);cout<<"»";
		    gotoxy(xx,yy);cout<<"¼";}
}

char ans;
char ask;
int life;
int main()
{
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;
if(life==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<<"_________";
}
else if(life==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.";
}
else if(life==3)
{
gotoxy(18,17);cout<<"[ Ask the audience ]";
gotoxy(10,23);cout<<"(a.) 23%   (b.) 52%   (c.) 13%   (d.) 12%";
}
}
else if(ask=='n')
gotoxy(10,30);cout<<"Your answer is: ";
cin>>ans;
if(ans=='a' || ans=='A')
gotoxy(14,35);cout<<"Correct!";
gotoxy(10,38);cout<<"Get ready for the next round!";
else
gotoxy(14,35);cout<<"Wrong!";
gotoxy(10,38);cout<<"Play again?";
getch();
return 0;
}

The problem is when I try to run the program it says:

"ERROR  Final.cpp 77: MISPLACED ELSE"

but i can't find if where is the error.

i'm so sorry, i'm not really good about this stuff. (^_^)

Recommended Answers

All 6 Replies

The problem is obvious :)
Let me show you some examples here:

if(false)
    std::cout << "We can't get here"; // This is the first action after the if, so this is what will be executed if the condition is met
std::cout << "We will get here,  because I'm not a part of the if statement."; // This has nothing to do with the if.
else // this is your problem... the else tries to match the if statement which may only be 1 action away.

The proper way to do it:

if(false)
{
    std::cout << "We can't get here!";
    std::cout << "This time you won't see me, because I'm inside the brackets connected to the if statement! :(";
}else{ // now the else will match the if statement on the first matching opening bracket.

So if() without brackets are fine if theres only going to happen one thing. But more than one action requires brackets.

Let me know if it helped :)

Your if s are going awry again
use {} every if , else if and else and many of your problems vanish without them if will only run until next ;

if(x == 1)
{//always use me
x = 3;
}//and me
else
{//here too
x = 4l
}//etc

Hi! I need help for my c++.

This is the code:

if(ans=='a' || ans=='A')
gotoxy(14,35);cout<<"Correct!";
gotoxy(10,38);cout<<"Get ready for the next round!";
else
gotoxy(14,35);cout<<"Wrong!";
gotoxy(10,38);cout<<"Play again?";

so

if(ans=='a' || ans=='A')
{
gotoxy(14,35);cout<<"Correct!";
gotoxy(10,38);cout<<"Get ready for the next round!";
}
else
{
gotoxy(14,35);cout<<"Wrong!";
gotoxy(10,38);cout<<"Play again?";
getch();
}

Your if s are going awry again
use {} every if , else if and else and many of your problems vanish without them if will only run until next ;

if(x == 1)
{//always use me
x = 3;
}//and me
else
{//here too
x = 4l
}//etc

so

if(ans=='a' || ans=='A')
{
gotoxy(14,35);cout<<"Correct!";
gotoxy(10,38);cout<<"Get ready for the next round!";
}
else
{
gotoxy(14,35);cout<<"Wrong!";
gotoxy(10,38);cout<<"Play again?";
getch();
}

Hi! thanks for helping me again. it works! but theres a problem, when i tried to run it and answered the "Your answer is: " it suddenly closed. I tried the 'system delay code and the tip that you have given to me last day but it still. ;'(

The problem is obvious :)
Let me show you some examples here:

if(false)
    std::cout << "We can't get here"; // This is the first action after the if, so this is what will be executed if the condition is met
std::cout << "We will get here,  because I'm not a part of the if statement."; // This has nothing to do with the if.
else // this is your problem... the else tries to match the if statement which may only be 1 action away.

The proper way to do it:

if(false)
{
    std::cout << "We can't get here!";
    std::cout << "This time you won't see me, because I'm inside the brackets connected to the if statement! :(";
}else{ // now the else will match the if statement on the first matching opening bracket.

So if() without brackets are fine if theres only going to happen one thing. But more than one action requires brackets.

Let me know if it helped :)

Hi! it works! Thanks for helpin me! (^_^) I have one more question, why everytime I run the program and answer the "Your answer is: " it suddenly closed? is there something wrong with my code?

Hi! it works! Thanks for helpin me! (^_^) I have one more question, why everytime I run the program and answer the "Your answer is: " it suddenly closed? is there something wrong with my code?

I did not read the logic correctly if
you change

final else:

else
{
  gotoxy(14,35);
  cout<<"Wrong!";
}
gotoxy(10,38);
cout<<"Play again?";
getch();
//add to end to delay instead of pause
char quit_c;
cout << "press any key to exit:" << endl;
cin >> quit_c;
cout << endl;
return 0;
}

there are a couple of issues with your code that should not be a problem but you have a set-up that is unfamilir to me.

It works when I run it on visual studio with some minor alterations but they won't fix your current error.

#include <iostream> //.h is for old c versions
#include <conio> //I don't actually use this

I have not used your positioning in the dos window
nor have i used getch() there is an obvious mistake in that you are marking
'a' as the correct answer but the answer is 'b'!

where you have

else if(ans == 'n')

you always still want to prompt for the answerso remove this line

I used endl in place of goto etc

std::cout << "who wants to be a millionaire" << std::endl;

you appear to have set-up that the std:: is default somewhere outside of the shown code so you wouldnt need it
this replaces your screen positioning and also empties the buffer.

//declare char c; somewhere in main
std::cin>>c; //in place of getch();

no gotoxy or box etc.. needed if you use line spacing

Other than that I have used your code unaltered and it runs
to the end with no problem on visual studio.

Come on, people. The solution is much more basic than anything posted here. The solution is

Learn to format your code! This link (if followed properly) will eliminate most else problems, bracket/parenthesis problems, and misread code problems. It makes your code easier to read, understandable, and maintaining the code is a snap.

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.