+ 3

Comparing string of text with if

Is it possible to compare strings with text is c++ using a if function?

9th Dec 2017, 1:29 PM
Helen Weygold
8 Respostas
+ 2
I have to say if is not a function, but yes, you can compare strings using if.
9th Dec 2017, 1:32 PM
Daniel Oravec
Daniel Oravec - avatar
+ 2
#include<string> #include<iostream> using namespace std; int main( ){ string str1 = "hi", str2 = "hi"; if(str1 == str2) { cout<< "They are equal"; } }
9th Dec 2017, 1:33 PM
Jonathan Álex
Jonathan Álex - avatar
+ 1
Thanks a million Jonathan! Can I ask if the user inputs the string eg yes or no is there a way to successfully check that?
9th Dec 2017, 1:35 PM
Helen Weygold
+ 1
#include<string> #include<iostream> using namespace std; int main( ){ string str1, str2 = "hi"; //now I ask user's input, expecting you to enter "hi" cin >> str1; //If user enters hi, it prints they're equal. Else, it prints they're different if(str1 == str2) { cout<< "They are equal"; } else { cout<< "They're different"; } } If it helps you, upvote and mark as the best answer :) Peace
9th Dec 2017, 1:40 PM
Jonathan Álex
Jonathan Álex - avatar
0
Thanks Daniel! Sorry about the misnomer.
9th Dec 2017, 1:33 PM
Helen Weygold
0
You can create strings normally and compare them as if they're numbers when you include string header
9th Dec 2017, 1:36 PM
Jonathan Álex
Jonathan Álex - avatar
0
Yes dude. I will write it. Please upvote it. It will help me a lot
9th Dec 2017, 1:37 PM
Jonathan Álex
Jonathan Álex - avatar
0
Will do.
9th Dec 2017, 1:38 PM
Helen Weygold