Whats wrong here? with my code C++

Started by
20 comments, last by c0uchm0nster 18 years, 5 months ago
Quote:Original post by DeathsBargin
*** Source Snippet Removed ***

1) I need to know how I make it, so that when they take the gun, gunnin = true.
2) Is there a way to make it so that it repeats the room description on its own on else. Or do I have to redo the description every time manually?
3) I want the while loop to make it loop forever. Is it the way it should be? If not how do I make it do so. (I need a loop to make the program not stop untill I tell it to(terminate)).

Thanks,

Death


Ok, good.

An infinite loop looks like this:
while (true){    //do your stuff ;)}

But might want to terminate it, so it could look like this:
while (true){    if (userQuits == true)        break;}

break; just tells the loop to stop.

I see you have this: while (dmover == false);

What you want is this:
while (dmover == false){    if (userQuits == true)        dmover = true;}


QUESTION #1:
You have something like this:
if (plan == 1){    cout<<"...in the room stuff...";}

You can simply something like this:
bool gunTanken = false;//or whateverif (plan == 1){    cout<<"You take the gun...";    gunTaken = true; // you make the gunTaken variable true, meaning that the                     // user took the gun...}


QUESTION #2:
I'm not sure exaclty how you want it. Here's a heavily commented example:
int dir = -1;do // a do {} while () loop...{ // begin it!    cout<<"Your description...\n\n";    // ask the user where he wants choice 1, 2,or 3    cout<<"Do you want to go:\n1) Left\n2) Right\n3) Up\n";    cin>>dir; // get the user's choice} while (dir != 1); // do this until the user enters 1


QUESTION #3
Well, if you want the game to repeat:
bool b_GameOver = false;char answer;while (b_GameOver == false){    // do your stuff ;)    cout<<"Quit? (y/n): ";    cin>>answer;    if (answer == 'y' || answer == 'Y')        b_GameOver = true;}

That would run and if the user says 'y' then then the loop would quit.

Hope I helped! [grin]
Advertisement
Why dont you just make it:

int gunin = 0;

and then when they pick up the gun make it:

gunin + 1;

Also, why are you making it loop back to the room part? It'll repeat it no matter what, even if they pick up the gun or clean the room... so instead of saying:

The room is now clean, but the gun remains untouched!
The room is now empty, you hold the silver gun in your hands!

It'll just repeat it. Does that make sense?
Quote:Original post by DeathsBargin
I tried it and something didnt work right (it didnt say the words like it was supposed to)
} else if (plan == 2 )
{cout<<"You sweep the dust off of the table with your hand, while swiping off the\nwalls you get a spider web stuck in your hand and you shake it off.\n";

}


Ok... Why do you have a '}' right in front, is that from the if()? Technically, theres nothing wrong with that. Exactly what happens?
Not sure i erased most of everything.. thanks for answering my questions. (though im sure there will be more)

death
*solved* (I think)
I dont know how you want it, but I'll give you a hint:

You have to compare everything, ex. plan == 2 || plan == 3... Doing plan == 2 || 3 would mean you're doing something like plan == true, or otherwise said, true == true, which would always execute. This is because anything other than 0 is true, since 0 is false. plan would equal true in that case, and since 2 is true and 3 is true, doing 2 || 3 would be doing true || true, which would be true, so then you compare if true is equal to true. A little confusing, but just remember not to do var == something || somethingelse, but rather var == something || var == somethingelse.

Forget what I just said, the logic is wrong. Doing something == somethingelse || anotherthing is wrong because if something is different than somethingelse is false, but anotherthing is true(it's different than 0) the whole if() would be true, since false||true is true.

I got mixed up with my C++ order of operations... [grin]
(the top turned out the same, but the middle of it wansn't right...)

Hope I helped.
Sorry about the triple post thing.

// This is in C++#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){    bool dmover= false;    bool gunin= false;    bool crmove= false;    while (dmover= false);    int plan;          cout<<"Hello\n";    cout<<"Welcome to the Demo!\n";    cout<<"This is test room 1.\n";      //Room Description        do {    cout<<"You are in a large dusty room, with a table, a gun on the table, and a door to your right.";     cout<<"What would you like to do?\n";    cout<<"1) Take the Gun.\n";    cout<<"2) Clean the room.\n";    cout<<"3) Go right.\n";    cin>>plan;    }while (plan !=2);    if (plan = 2) {             cout<<"You swipe the room clean with your hand.\n";    // that was so that it says the description as dusty untill he says dusts the room        do {    cout<<"You are in a large room, with a table, and a door to your right.\n";    cout<<"What would you like to do?\n";    cout<<"2) Clean the room.\n";    cout<<"3) Go right.\n";    cin>>plan;    }while (plan !=1);    if (plan = 1) {    cout<<"You take the gun off of the table.\n";                                                                                         system("PAUSE");     return EXIT_SUCCESS;}

I decided to take a new approavh with the room description..
Giving me errors about the very last bracket ( }) at the very bottom.
I see... Well, first, in some places you have:
if (aVar = anotherVar){}

You use the '==' to compare, not the '='... That means that aVar no equals anotherVar, and if anotherVar wasn't 0 (thus, false) the if would always execute.

I cant stress this enough... Why do you have an infinite loop in there:
while (dmover == false);

???

And second, you have an unterminated if(). You have:
if (plan = 1) {    cout<<"You take the gun off of the table.\n";                                                                                         system("PAUSE");

Where is the '}' in there?
Im still getting the same error. What undetermined variable are you talking about?

// This is in C++#include <cstdlib>#include <iostream>using namespace std;int main(int argc, char *argv[]){    bool dmover= false;    bool gunin= false;    bool crmove= false;    int plan;          cout<<"Hello\n";    cout<<"Welcome to the Demo!\n";    cout<<"This is test room 1.\n";      //Room Description        do {    cout<<"You are in a large dusty room, with a table, a gun on the table, and a door to your right.";     cout<<"What would you like to do?\n";    cout<<"1) Take the Gun.\n";    cout<<"2) Clean the room.\n";    cout<<"3) Go right.\n";    cin>>plan;    }while (plan !=2);    if (plan == 2) {             cout<<"You swipe the room clean with your hand.\n";    // that was so that it says the description as dusty untill he says dusts the room        do {    cout<<"You are in a large room, with a table, and a door to your right.\n";    cout<<"What would you like to do?\n";    cout<<"2) Clean the room.\n";    cout<<"3) Go right.\n";    cin>>plan;    }while (plan !=1);    if (plan == 1) {    cout<<"You take the gun off of the table.\n";                                                                                            system("PAUSE");     return EXIT_SUCCESS;}
Quote:Original post by DeathsBargin
Im still getting the same error. What undetermined variable are you talking about?

*** Source Snippet Removed ***


On the end, you have:
if (plan == 1) {    cout<<"You take the gun off of the table.\n";

And then, just after that, you have system("pause");. You need to end that if() with a '}', as so:
if (plan == 1) {    cout<<"You take the gun off of the table.\n";} // <---- here it is


Good luck!

This topic is closed to new replies.

Advertisement