Check if input contains a certain string.

robinremue

Member
Reaction score
16
I'm new at this but how do I check if the answer on a question contains a certain string.

For example, if the answer on a question is yes, a certain output shows up, if it is not yes, something else shows up.

I got something like:

Code:
if (string = "yes") {
                     cout <<  "whatever"; }
else if (string = "no") {
                     cout << "whatever"; }

but ofc it doesn't work and I'm quite sure it can't be this easy...

Thx in advance =)
 

Weegee

Go Weegee!
Reaction score
102
First of all, you will never know if you are hitting yes or no because they are both the same. Second, I think the curly brackets are in the wrong place. Plus it could make life easier for everyone if you can put the language you are using. C++? C? :p sorry if I seemed harsh
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
but ofc it doesn't work and I'm quite sure it can't be this easy...
I'd start out by using == instead of =.


Second, I think the curly brackets are in the wrong place. Plus it could make life easier for everyone if you can put the language you are using. C++? C? :p sorry if I seemed harsh

I see nothing wrong with these curly braces: the compiler doesn't care about whitespaces. As for the language, it's obvious: streams are a C++ exclusive.
 

Samael88

Evil always finds a way
Reaction score
181
Code:
if (string == "yes") {
                     cout <<  "whatever"; }
if (string == "no") {
                     cout << "whatever"; }
This might be what you are looking for.
= is to set a value.
== is a boolean operator which means "equal to".

And for what I know, there is know "else" in c++:eek:

Edit: crap.. enouwee beat me to it:( See enouwee, I have been learning:p
 

robinremue

Member
Reaction score
16
I'd start out by using == instead of =.




I see nothing wrong with these curly braces: the compiler doesn't care about whitespaces. As for the language, it's obvious: streams are a C++ exclusive.

== solved the problem! =)

Thank you very much!
 

Samuraid

Advisor
Reaction score
81
As a side note, calling your string variables "string" is a bad idea in C++. If the compiler actually allows it, at the minimum in causes much confusion for anyone reading your code. :p
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top