• Advertisement
Sign in to follow this  

Help with c++ and strings

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

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
Quote:
Original post by kingpinzs
Is there a way that you can do a if statment and have no input or the input is blank?
example
*** Source Snippet Removed ***


Why not just use an else statement?

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
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?


You'd look up the std::istream and std::string classses in a C++ reference manual, because they are the respective types of your std::cin and string objects.

Quote:
Original post by Rhaal
Why not just use an else statement?


Because that would be triggered by *any* string that doesn't match the earlier cases, not just an empty string.

Share this post


Link to post
Share on other sites
Just a quickie, for an any length of string input, have you thought about using
getline (cin, input, '#')
just a thought then use a while and run through each character, may not be the most efficient way, but it will work to get you started, then process the string until lastChar == '#'...

Hope this is of some use,
Add.

Share this post


Link to post
Share on other sites
Sorry about that. I've not been working with C++ strings for some time (Java damage). But I actually looked back at my code and I use the equal even do string apprently has a overloaded version.

Share this post


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

  • Advertisement