Ive been having a problem, ive gone through the frst few basic lessons in c++ here at the site, and can get my tiny programs to accept a single character or number, but it will not accept any whole words. How do I get it to do so?
Ive been having a problem, ive gone through the frst few basic lessons in c++ here at the site, and can get my tiny programs to accept a single character or number, but it will not accept any whole words. How do I get it to do so?
Code:#include <iostream> #include <string> using namespace std; int main() { string word; cout<<"enter a word: "; cin>>word; cout<<"you entered: "<<word<<endl; return 0; }
thanks! i know im a n00b but it really helps anyway
okay, i have this now... it wont do squat, and i know i have absolutely nothing good here, but maybe you can see what im trying to do, and understand why im an idiot, btw, my cout is just random crap, so um, iforget my point then
Code:#include <iostream> #include <string> using namespace std; int main(void) { string name; string choicea = 'leave' ; cout<<"Welcome to Texst adventure! Please enter your name:" ; cin>> name; cin.ignore(); cout<< " your name is: "<< name <<"!\n\n"; cin.get(); cout<<"welcome to the adventure,"<< name <<"!\n"; cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n"; cin>> choicea; cin.ignore(); if (choicea == 1){ cout<<" You exit your house, now what?\n";} else ( cout<<" I do not understand your command, please try again. \n";} while ( choicea != 1 ){ } }
Also you're comparing a string to an int in your if statement.Code:string choicea = "leave"; // Double quotes
Sent from my iPadŽ
Originally Posted by futurama140
That is there for seemingly no apparent reason you might want to take it out.
My computer is awesome.
so how do i compile the string into a string into the if statement?
Also, when i type in "leave" when running the program, after thethe program exits, does anyone get what im trying to do? i want it to display the one cout after i type in leave, and if i dont type in "leave" i want it to say the other cout.Code:cin>> choicea;
Help please?
So, if choicea is equal to "leave", you want it to run the first cout? Then code: if choicea is equal to "leave".
Your current code says: if choicea is equal to 1.
no, ichanged it earlier, before i made the post with the question, my current code is:Code:#include <iostream> #include <string> using namespace std; int main(void) { string name; string choicea = "leave" ; cout<<"Welcome to Texst adventure! Please enter your name:" ; cin>> name; cin.ignore(); cout<< " your name is: "<< name <<"!\n\n"; cin.get(); cout<<"welcome to the adventure,"<< name <<"!\n"; cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n"; cin>> choicea; if (choicea == "leave"){ cout<<" You exit your house, now what?\n";} else { cout<<" I do not understand your command, please try again. \n";} while ( choicea != "leave" ){ } }
OK, so I got that one problem fixed, but now how do i loop it back the the first choice if someone inputs an unrecognised command? i have:notice it says while choicea does not equal "leave" then its blank, i dont know what to say to get it to loop back to the choice.Code:#include <iostream> #include <string> using namespace std; int main(void) { string name; string choicea = "leave" ; cout<<"Welcome to Texst adventure! Please enter your name:" ; cin>> name; cin.ignore(); cout<< " your name is: "<< name <<"!\n\n"; cin.get(); cout<<"welcome to the adventure,"<< name <<"!\n"; cout<<"You are a new warrior recruit in the Hyllian army! Starting out in your hut, you recieve a letter telling you to go to the place. What do you do?\n"; cin>> choicea; if (choicea == "leave"){ cout<<" You exit your house, now what?\n"; cin.get();} else { cout<<" I do not understand your command, please try again. \n";} while ( choicea != "leave" ){ } cin.get(); }
edit: also, it now wont display the 2nd cout if i input the wrong text. it ggoes to the first cout anyway
Last edited by futurama140; 12-08-2005 at 01:05 AM.
something like this
KurtCode:cin>> choicea; while (choicea != "leave"){ cout<<" I do not understand your command, please try again. \n"; cin>> choicea; } cout<<" You exit your house, now what?\n";
yea, thats not cutting it, you try using that in my code and see what happens, and its not pretty
Actually, that looks like it would fit nicely. Maybe you didn't use it correctly. Post your code and your problems if your imlpementation of the suggestion isn't working.