-
If statement's
Here's the code(if you could call it that)
Code:
#include <iostream.h>
int main()
{
int input;
cout<<"1. play game: ";//mabey no two dot's?
cout<<"2. Load game: ";
cout<<"3. play multiplayer: ";
cout<<"4. Exit: ";
cin>>input;
{
if(input==1)
cout<<"Play on";
if(input==2)
cout<<"Your game is loading";
if(input==3)
cout<<"Play multiplayer, your gonna go down";
if(input==4)
cout<<"wooooos";
else
cout<<"program over";
}
return 0;
}
The problem is with the executable when it's run, when you input 1,2,3, or 4 it display's the text given with cout, but it also display's
the Else "program over" even when i'm inputting 1,2,3,or 4.
So if anyone feel's like it answer me that. Thank's
loopy
-
aaah
Try to use: else if
Im a newb too. But I think it will work..
Tell me if it works=)
Code:
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
-
else if
No unfortunatly that make's it not even compile.
-
Strange.
Code:
#include <iostream.h>
int main()
{
int input;
cout<<"1. play game: ";//mabey no two dot's?
cout<<"2. Load game: ";
cout<<"3. play multiplayer: ";
cout<<"4. Exit: ";
cin>>input;
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
return 0;
}
This code works on my compiler.. I use Dev C++ 4
-
The problem with a series of if statements and then a final else condition is that it doesn't perform quite as you might expect. Think of each if statement as its own entity and those entities can only have one else. So 1, 2, and 3 are tested but it any of those is true, the final if will fail and the else will also be executed, resulting in the unusual output. This works on my system:
Code:
#include <iostream.h>
int main()
{
int input;
cout<<"1. play game: ";//mabey no two dot's?
cout<<"2. Load game: ";
cout<<"3. play multiplayer: ";
cout<<"4. Exit: ";
cin>>input;
{
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
}
return 0;
}
-Prelude
-
Code:
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int input;
cout<<"1. play game: ";//mabey no two dot's?
cout<<"2. Load game: ";
cout<<"3. play multiplayer: ";
cout<<"4. Exit: ";
cin>>input;
{
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
}
getch();
return 0;
}
this should compile, what compiler are you using? Also you might want to try a switch statement
Code:
cin >> input;
switch (input) {
case 1:
cout<<"Play on";
break;
case 2:
cout<<"Your game is loading";
break;
case 3:
cout<<"Play multiplayer, your gonna go down";
break;
case 4:
cout<<"wooooos";
break;
default:
cout<<"program over";
}
-
remove the { }
Thats the problem in your code..
{ //Should not be there
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
} // neither should it be here.
-
>Thats the problem in your code..
That's not a problem, you can place a block anywhere you want in your program. Free floating blocks aren't very common, but they are quite legal.
-Prelude
-
My bad thing, hehe
I'm actually at the case switch area of the tut on this site and it say's to mentally put in if statment's in the place of the case switch, so i decided to write this little prog. this worked
Code:
#include <iostream.h>
int main()
{
int input;
cout<<"1. play game: ";//mabey no two dot's?
cout<<"2. Load game: ";
cout<<"3. play multiplayer: ";
cout<<"4. Exit: ";
cin>>input;
{
if(input==1)
cout<<"Play on";
else if(input==2)
cout<<"Your game is loading";
else if(input==3)
cout<<"Play multiplayer, your gonna go down";
else if(input==4)
cout<<"wooooos";
else
cout<<"program over";
}
return 0;
}
thank's alot you guy's.
I'm useing djgpp(could'nt get borland to work)
The if statment's section does'nt mention "else if"
There's probibly alot more it doesn't mention..my..head is ...getting filled with...fuzzyness.
thank's
-
imo
from what i've seen case statements are much easier to use for something like what your describing, so u might want to get in the habit of using them anywayz.
-
Now this is what i call bringing up the dead. Die, thread, die.......