Having trouble compiling code for a small text adventure game

Pages: 12
cin >> action;

if (action == 1, 2, 3, 4, 5, 6, 7)
{
cout << "code";
}

if (action != 1, 2, 3, 4, 5, 6, 7)
{
return 0;
}

Essentially when the user comes to a point they are asked to input a number corresponding to an action.

The main problem I find is that if people simply smash in "sodingsidg" or characters, it plays through the rest of the script for some reason.

so i added the if if (action != 1, 2, 3, 4, 5, 6, 7), to stop the program if they don't input one of the numbers.

The problem I'm finding is that if I input 1 it simply returns 0; anyway.

It displays the code, but it also does the return 0;

Why is that, should the '!=' operator only mean that it should only return 0; 'if' the input is NOT 1.

Any tips on how to solve this?
I do wonder where people get the idea that , is some sort of OR.

http://en.wikipedia.org/wiki/Comma_operator

Basically, all the condition says is if (7).

What you need to do is if (action>=1 || action<=7){/*...*/}else{/*...*/}
The else block executes only if the expression evaluates to zero.

When you need to check that the value is one of a list, there's quite a few things you can do, but the simplest is if (value==something || value==some_other_thing || value==some_other_other_thing)
You should take some time to study the difference between &&, ||, !, &, |, ~, and ^, and their precedences. It will come in handy at one point or another.
Last edited on
because
 
if (action != 1, 2, 3, 4, 5, 6, 7)

is illegal

you have to type out the hole condition(which is lame)
 
if(action!=1||action!=2||action!=3 ect...)

or
1
2
3
4
5
int input;
cin>>input;
if(input<=7&&input>0)
cout<<"code"<<endl;






Last edited on
jloundy: Man, you suck. Not only is that absurdly slow, it's not even correct.
So does your mom
Last edited on
eh? Calm down buddy....
You may wish to use a switch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch( action ){

case 1:
    //...
    break;

case 2:
    //...
    break;

//... 3 to 7

default:
    // Not in [1,7], do what thy wilt
}
lol
Ok I went with the switch setup so the program can recognize if the user inputs 1 through 7.
But that wasn't really the issue.

I need to limit the input to 1 through 7, and if anything else is put in I need it to execute a different code.

What I have;

cin >> action;
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
}

cout << "stuff";
cout << "stuff";
if (stuff == stuff)
{
cout << "more stuff";
}

I tried the 'if(action!=1||action!=2||action!=3 ect...)' line but it doesn't work, if you input something other then 1-7 it still executes code till you get to the if. EDIT: for example if you input 'asf' it just skips and goes to the next statement.

I would use breaks as well, but if I don't have a line to execute something if the user inputs something other then 1-7 then there's no point in sticking a break in anywhere.

Any ideas?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
   {
   //Your code for conditions 1-7
   break; // dont forget this
   }
default :
   {
   // code for any other exception
   break;
   }
}
Illegal default,

should it not be inside the second set of '{}'s?

EDIT: Nope tried inside the second.

any ideas?
Last edited on
my syntax is correct,
double check your code, make sure you are using ':' not ';'

if it make you feel better then just forget the '{}'s :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
   //Your code for conditions 1-7
   break; // dont forget this
default :
   // code for any other exception
   break;
}
Last edited on
Alrite I've just got one more gripe (lol sorry)

Ok the illegal default error is gone,

and if the user inputs gibberish, ie. "sodigjisodhg". it will go to 'default'

So that's exactly what I was aiming for there, so thank you V MUCH AMIGOS for helping me solve that.

but if the user enters 1 through 7, the 'default' commands run anyway.

Are there any tags i can add to the default to prevent it running in the case of an input of 1-7?

Here's what I have;

cin >> action;
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :

cout << "stuff";

default :
{
cout << "end message";
cin >> restart;
}

then: rest of the game

Is there any way I can get it to hop over the default command if the user enters 1-7?

You NEED the break;. It is why you can do this:

1
2
3
4
5
6
case 1:
case 2:
//blah blah
break;
case 3:
//blah blah 


and have it be different from:

1
2
3
4
case 1:
case 2:
case 3:
//blah blah 
Ok so now I have;

cin >> action;
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :

cout << "stuff";

break;

default :
{
cout << "end message";
cin >> restart;
}

:: rest of program ::

I still have the same problem, if the user inputs 1 through 7 it just executes the default code and the program exits.
Can you post the rest of your code? That doesn't provide enough info.
int lose;
int action;
int action2;
int action3;
int action4;
int action5;
int A = 1;
int B = 2;
int C = 3;
int D = 4;
int E = 5;
int F = 6;
int G = 7;
long exit;

cout << tus;
cout << tuss;
cout << tusz;
cout << tus1;
cout << tus2;
cout << tus3;
cout << tus4;
cout << tus5;
cout << tus6;
cout << tus7;
cout << tus8;
cout << tusa;
cout << tusb;
cout << tusc;
cout << tusd;
cout << tuse;
cout << tusf;
cout << tusg;
cout << tusr;
cout << tus9;
cout << tus10;

cout << "Welcome to TUS, your objective is to defeat the forum guardians at the sub-section gates, let the rape begin ...\n";
cout << "\n";

cout << "Username login: ";
cin >> user;

cout << "Enter Password: ";
cin >> pass;

cout << "\n";

cout << "Welcome to the forums " << user << ".\n";

cout << "Please input the corresponding forum number .. \n\n";

cout << ":: +1+ Site Wank ::\n:: +2+ Metal Gear Shit ::\n:: +3+ Metal Gear Shit II ::\n:: +4+ Movies and Shit ::\n:: +5+ Other Games ::\n:: +6+ General Shit ::\n:: +7+ Spam Board ::\n";
cout << "\n";

cin >> action;
switch ( action )

{
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :



cout << "\n" << "Uhh oh a wild Error1355 appeared\n";
cout << "\n";

cout << error1;
cout << error2;
cout << error3;
cout << error4;
cout << error5;
cout << error6;
cout << error7;
cout << error8;
cout << error9;
cout << errora;
cout << errorb;
cout << errorc;
cout << errord;
cout << errore;
cout << errorf;
cout << errorg;
cout << errorh;
cout << errori;
cout << errorj;
cout << errork;
cout << errorl;
cout << errorm;
cout << errorn;
cout << erroro;
cout << errorp;

cout << "\n";

cout << "What do you do?\n";
cout << "\n";

cout << "+1+ Begin flaming FULL FORCE\n+2+ Try to reason with it\n+3+ Fat Joke\n+4+ Ignore it and just dump buckets of cum down it's ass\n";
cout << "\n";
cout << "Input number corresponding to action: \n";
break;


default:
{
cout << "Please enter the correct number dipshit!!\n";
cout << "Type 'exit' and hit return to exit\n";
cin >> lose;
return 0;
}

cin >> action2;
if (action2 == 1)
{
cout << "\n";
cout << "Calculating victory or defeat ... \n";
cout << "\n";
cout << "...\n";
cout << "....\n";
cout << ".....\n";
cout << "......\n";
cout << "\n";
cout << game1;
cout << game2;
cout << game3;
cout << game4;
cout << game5;
cout << game6;
cout << game7;
cout << gamef;
cout << game8;
cout << game9;
cout << gamea;
cout << gameb;
cout << gamec;
cout << gamed;
cout << gamee;
cout << "\n";
cout << "\n";
cout << "Hit any key and enter to exit ..";
cin >> lose;
return 0;
}

if (action2 == 2)
{
cout << "\n";
cout << "You plead with Error1355 for you life .. \n";
cout << "..............\n";
cout << ".............\n";
cout << "............\n";
cout << "...........\n";
cout << "..........\n";
cout << ".........\n";
cout << ".......\n";
cout << "\n";
cout << "*He smushes you*\n";
cout << "\n";
cout << game1;
cout << game2;
cout << game3;
cout << game4;
cout << game5;
cout << game6;
cout << game7;
cout << gamef;
cout << game8;
cout << game9;
cout << gamea;
cout << gameb;
cout << gamec;
cout << gamed;
cout << gamee;
cout << "\n";
cout << "\n";
cout << "Hit any key and enter to exit ..";
cin >> lose;
return 0;

}

if (action2 == 3)
{
cout << "\n";
cout << "You think hard... and make a terrible fat joke .. \n";
cout << "+++\n";
cout << "+++++\n";
cout << "++++++\n";
cout << "+++++\n";
cout << "+++\n";
cout << "\n";
cout << "(" << user << ")" << " +b\n";
cout << "\n";
cout << "*Disconnected from server. Banned for 5 minutes (unfunny fat joke)~DHRH-ADF.com*\n";
cout << "\n";
cout << game1;
cout << game2;
cout << game3;
cout << game4;
cout << game5;
cout << game6;
cout << game7;
cout << gamef;
cout << game8;
cout << game9;
cout << gamea;
cout << gameb;
cout << gamec;
cout << gamed;
cout << gamee;
cout << "\n";
cout << "\n";
cout << "Hit any key and enter to exit ..";
cin >> lose;
return 0;

}

if (action2 == 4)
{
cout << "\n";
cout << "You try bitterly to ignore the massive TUS Admin infront of you..\nIt doesn't seem to be working much .....\n\nYou grab a bucket of cum and do the obvious\n\nHe begins to tell you something about Castlevania 3 but you don't care and continue on ..\n\n";

cout << win1;
cout << win2;
cout << win3;
cout << win4;
cout << win5;
cout << win6;
cout << win7;
cout << win8;
cout << win9;
cout << wina;
cout << "\n";
cout << "\n";


}

cout << "\n";
cout << "You continue your browsing through the forums .. \n";
cout << "\n";
cout << "Browsing the threads in forum no." << action;
cout << "\n";
cout << "\n";
cout << "All of a sudden a robot rumbles into view\n";
cout << "It looks like that robot from the movie 'WALL-E'\n";
cout << "... but it has 'WALLEYE' sprayed on the side of it instead ..\n";

cout << "\n";
cout << "\n";

cout << walleye1;
cout << walleye2;
cout << walleye3;
cout << walleye4;
cout << walleye5;
cout << walleye6;
cout << walleye7;
cout << walleye8;
cout << walleye9;
cout << walleyea;
cout << walleyeb;
cout << walleyec;
cout << walleyed;
cout << walleyee;
cout << walleyef;
cout << walleyeg;
cout << walleyeh;
cout << walleyei;
cout << walleyej;
cout << walleyek;
cout << walleyel;
cout << walleyem;
cout << walleyen;
cout << walleyeo;
cout << walleyep;
cout << walleyeq;
cout << walleyer;
cout << walleyes;
cout << walleyet;
cout << walleyeu;
cout << walleyev;
cout << walleyew;
cout << walleyex;
cout << walleyey;
cout << walleyez;
cout << walleyeaa;
cout << walleyeab;
cout << walleyeac;
cout << walleyead;
cout << walleyeae;
cout << walleyeaf;
cout << walleyeag;
cout << walleyeah;
cout << walleyeai;
cout << walleyeaj;
cout << walleyeak;
cout << walleyeal;
cout << walleyeam;
cout << walleyean;
cout << walleyeao;
cout << walleyeap;
cout << walleyeaq;
cout << walleyear;
cout << walleyeas;
cout << walleyeat;
cout << walleyeau;
cout << walleyeav;
cout << walleyeaw;
cout << walleyeax;
cout << walleyeay;
cout << "\n";
cout << "\n";

That is literally what I have for that section.

I'd post the whole thing but it's over 42,000 characters, and the limit is 8192.
I... I'm speechless.
o,o

ya

EDIT: As you may have noticed this is sort've an in-joke thing.

Also the big walls of cout are all predefined strings.
Last edited on
Uh.... wtf is all this?
Pages: 12