Public Group

Whats wrong here? with my code C++

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

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 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.

if (plan == 1);
{cout<

Share on other sites
Thanks i fixed that. (Thought I am sure there will be more problems soon to come.

Death

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 on other sites
thanks.. Ill be back with more questions as they come.

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 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 on other sites
Thanks that will help.. (Hadnt thought about that) :)

Still need those other questions answered please. :P

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

}

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 11
• 20
• 12
• 11
• 41
• Forum Statistics

• Total Topics
631402
• Total Posts
2999870
×