Monday, July 27, 2009

How can I get this C++ program to work?

This is my C++ code but it doesn't seem to like it. Help please!!





/*Uber Cool Text Based Adventure


Version 1 starting implementing code */








#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


int main()


{


// Command variable


string UserInput;





// Item variables


bool bottle;





// Prison





cout %26lt;%26lt; "You are in a prison cell. \nYou cannot remember why you are here. \nYou see a bottle on the floor. \n";


cout %26lt;%26lt; "Enter a command: ";


cin %26gt;%26gt; command;


cout %26lt;%26lt; command;





if (UserInput == "pick up bottle")


{


cout %26lt;%26lt; "You pick up the bottle";


bottle = true;


cout %26lt;%26lt; bottle;


}








return 0;








}

How can I get this C++ program to work?
Well firstly the main problem that your going to have is with the variable "command". This variable has not been defined. I think what you want is this :





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;


int main()


{


// Command variable


string UserInput;





// Item variables


bool bottle;





// Prison





cout %26lt;%26lt; "You are in a prison cell. \nYou cannot remember why you are here. \nYou see a bottle on the floor. \n";


cout %26lt;%26lt; "Enter a command: ";


cin %26gt;%26gt; UserInput


cout %26lt;%26lt; UserInput;





if (UserInput == "pick up bottle")


{


cout %26lt;%26lt; "You pick up the bottle";


bottle = true;


cout %26lt;%26lt; bottle;


}








return 0;


}
Reply:You haven't declared command, although I'm not sure that one needs to. It would help to know the error message.





Make sure you're using the correct syntax for the compiler - gcc likes different things to CC for example.
Reply:You haven't declared command. You want to change it to





cin %26gt;%26gt; UserInput;


cout %26lt;%26lt; UserInput;
Reply:Okay, there is one major oops and several that will develop soon if not addressed now. First, you declare a string variable called Userinput with a comment above it and try to use the word Command for storing and comparing the responses. Not good. Change it so the cin statement uses the same named string variable, as in 'cin %26gt;%26gt; Userinput;' . Next up, I'd recommend breaking some of the functionality out into, well, functions. It'll keep the code neater and be easier to track down errors. And last, but way not least, generally it is better to over use comments than to not comment something enough. Describe and annotate *EVERYTHING*. I hope this helps you out.
Reply:You haven't declared some of command in your code. need to fix the and also need to compile
Reply:while since i used c++ but think you need another library adding at the top - #include %26lt;stdlist%26gt; believe you might also need the conio.h library as well

hawthorn

No comments:

Post a Comment