cin>>input;
if (input == gun)
{
cout<<"you win";
}
if (input =="run")
{
cout<<"you run like a cowerd";
}
if (input == "" )
{
cout<<(107 % afterBattle)
<< " coins.";
}
Help with c++ and strings
Is there a way that you can do a if statment and have no input or the input is blank?
example
Yea, you'd have to do exactly what you're doing. Test for the cases you want to consider.
Assuming input is a STL string.
if(input == "run"){ // Process run.}if(input == gun){ // Process win.}if(input == "" || input.length() == 0){ // Process default.}
Assuming input is a STL string.
You can't compare to string in that maner.
The operator "==" onöy compares the native reference and only work on a single char or a integer that has native span of memory. If you have a string you should use String::equal() or in c use strcmp because of that fact.
if(str == "mycompare") //not valid and will give you false always
if(strcmp(str,"mycompare")) //valid usage of method
if(str.equal("mycompare")) //valid utilizing the std library..
The operator "==" onöy compares the native reference and only work on a single char or a integer that has native span of memory. If you have a string you should use String::equal() or in c use strcmp because of that fact.
if(str == "mycompare") //not valid and will give you false always
if(strcmp(str,"mycompare")) //valid usage of method
if(str.equal("mycompare")) //valid utilizing the std library..
Quote:Original post by MikaelBrandin
You can't compare to string in that maner.
The operator "==" onöy compares the native reference and only work on a single char or a integer that has native span of memory. If you have a string you should use String::equal() or in c use strcmp because of that fact.
Not true; operator== is overloaded appropriately for std::string.
cin>>input; if (input == "gun") { cout<<"you win and take all 107 peices of gold"; } if (input =="run") { cout<<"you run like a cowerd"<<endl; } if (input.empty()) { cout<<(107 % afterBattle) << " coins."; }
This does not work. I just Want to press enter if they dont want to do one of the choices. Does any one else Have any ideas?
getline (cin, input);if (input == "gun") { cout << "you win and take all 107 peices of gold";} else if (input =="run") { cout << "you run like a cowerd" << endl;} else if (input.empty()) { cout << (107 % afterBattle) << " coins.";}
Does this work?
Oxyd
now it is not waiting for me to push enter it sees the input as empty and just keeps going. I need it to wait for me to press enter
Then put a cin.ignore(); just before the getline. That is normal behavior if you have used a cin>> before a getline (as >> does not consume the whitespace);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement