• Advertisement
Sign in to follow this  

Help with c++ and strings

This topic is 4930 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

Is there a way that you can do a if statment and have no input or the input is blank? example
     cin>>input;
       if (input == gun)
       {
           cout<<"you win";
       }
       if (input =="run")
       {
           cout<<"you run like a cowerd";
       }
       if (input == "" )        
       {
       cout<<(107 % afterBattle)
       << " coins.";
       }

Share this post


Link to post
Share on other sites
Advertisement
Yea, you'd have to do exactly what you're doing. Test for the cases you want to consider.


if(input == "run")
{
// Process run.
}

if(input == gun)
{
// Process win.
}

if(input == "" || input.length() == 0)
{
// Process default.
}


Assuming input is a STL string.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

Share this post


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

Share this post


Link to post
Share on other sites
Thanks that works great. Now if I were to research this how in the world would I even found this info any were else? How would I know what to look up or keywords to use?

thanks agin It works and that is exactly what I needed

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement