Sign in to follow this  

Whats wrong here? with my code C++

This topic is 4419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


// 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";
    cout<<"You are in a large dusty room, with a table, a gun on the table, and a door to your right.";  //Room Description
    cout<<"What would you like to do?\n";
    cout<<"1) Take the Gun.\n";
    cout<<"2) Clean the room.\n";
    cout<<"3) Go right (Through the door).\n";
    cin>>plan;
    if (plan == 1);
      {cout<<"You take the gun.\n"};
      else {cout<<"Nothing happens\n"};
     
    
    
    
    system("PAUSE"); 
    return EXIT_SUCCESS;
}


Whats wrong with it? Its sayign theres something wrong with {

Share this post


Link to post
Share on other sites
Your if statement should look more like:

if (plan == 1) {
cout<<"You take the gun.\n";
} else {
cout<<"Nothing happens\n";
}

You had a semi-colon after the if (plan == 1) which makes so that if the plan was equal to 1 it would do nothing, and then you had semi-colons after the } instead of after the statements.

Share this post


Link to post
Share on other sites
When you have an if() you should have it like this:

if(anExpression)
{ //curly brace 1
//do stuff
} //curly brace 2


You can a ; on the end of the if(), so that means that the if() does nothing, since just a ; means an empty statememt. The second thing is that the { and the } declare a new scope. So you dont put a ; on the end, just like you don't put a ; on the end of ther main() {}. You have to put the ; on the statement, just like you have in the beginning.

P.S. You also have an infinite loop in there:

while(dmover == false);

That's an empty statement, just like I said with the 'if'. You should have it more like:

while(dmover == false)//; - not here...
{//not here either...
//do your stuff ;)
}//no here, either...

Share this post


Link to post
Share on other sites


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";
cout<<"You are in a large dusty room, with a table,and a door to your right."; //Room Description
cout<<"What would you like to do?\n";
cout<<"1) Take the Gun.\n";
cout<<"2) Clean the room.\n";
cout<<"3) Go right (Through the door).\n";
cin>>plan;
if (plan == 1) {
cout<<"You take the gun.\n";
} else {
cout<<"You are in a large dusty room, with a table,and a door to your right\n";
}
if (gunin= true) {
cout<<"You are in a large dusty room, with a table,and a door to your right.";
} else {
cout<<"You are in a large dusty room, with a table,and a door to your right.";
}



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

Share this post


Link to post
Share on other sites
Just a suggestion, if you don't like it ignore this post...

You have if (plan == 1) {/*do stuff*/} else {/*do stuff*/}. But you give 3 choices to the user... The else means that plan != 1... But what if the user puts in something like 5? That wasnt in the choices, so you can do something like this:

if (plan == 1) // if the plan is 1
{
//do stuff
}
else if (plan == 2 || plan == 3) // if the plan is the other choices
{
//do stuff
}
else // now... if the choice is not any of them...
{
std::cout<<"\n'"<<plan<<"' isn't one of the choices..."<<std::endl;
}

Good luck!

Share this post


Link to post
Share on other sites
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";

}



Share this post


Link to post
Share on other sites
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 whatever

if (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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

do {
// some stuff
}while (plan !=2);

// THIS IS NOT REQUIRED
// if we are here, then plan MUST be 2
// otherwise the loop above would run again
// so i commented it out


//if (plan == 2) {
// also note this ^ '{' was never terminated with a '}'
// this was probably the compiler error

cout<<"You swipe the room clean with your hand.\n";

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);

// again, you dont need this if (plan == 1)
// if we are here, then plan MUST be 1
// otherwise the loop above would run again
// so i removed it

cout<<"You take the gun off of the table.\n";

system("PAUSE");
return EXIT_SUCCESS;
}





if youre having any more problems then post the errors you're getting too.

good luck!

Share this post


Link to post
Share on other sites
DeathsBargain - all of your questions have had to do with the syntax of if statements and loop statements - I suggest you find an article/tutorial/something that explains these things and really be sure you understand what every single character does in them.

Another thing that may really help you, which I always do when I program, is to make the skeleton of each statement before I start making the content. I'll use your latest code for an example.

First I'd create the structure of the int main function:

int main(int argc, char *argv[])
{
}

Then I'd fill that in with it's content. When I got to the first occurrance of (in this case) the do-while statement, I'd create it's skeleton:

int main(int argc, char *argv[])
{
//code before the do-while
do {
} while (plan!=2);
}

Then I'd fill in the contents of that loop and continue this process for the rest of the function...
In other words, I try to create the opening and closing of every block statement (the curly braces stuff) BEFORE I write the contents of that statement, that way I never forget to close the statement, because it's already closed before I even start writing it's contents...

Share this post


Link to post
Share on other sites

This topic is 4419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this