+ 3
Comparing string of text with if
Is it possible to compare strings with text is c++ using a if function?
8 Respostas
+ 2
I have to say if is not a function, but yes, you can compare strings using if.
+ 2
#include<string>
#include<iostream>
using namespace std;
int main( ){
string str1 = "hi", str2 = "hi";
if(str1 == str2)
{
cout<< "They are equal";
}
}
+ 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?
+ 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
0
Thanks Daniel! Sorry about the misnomer.
0
You can create strings normally and compare them as if they're numbers when you include string header
0
Yes dude. I will write it. Please upvote it. It will help me a lot
0
Will do.