why if statement is going wrong < act as > and > act as <? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why if statement is going wrong < act as > and > act as <?

#include <iostream> #include <string> using namespace std; main(){ string a="hello"; string b="everyone"; if(a>b){ cout<<a;} }

14th Dec 2017, 1:08 PM
Frankestine Stine
Frankestine Stine - avatar
1 Answer
+ 1
first of all add int main() and return 0; at the end second thing, don't compare strings using relational operators because it compares the letters, if the letter of first string has greater ascii code it will be termed as greater. you can use a string function as a.compare(b) and if you want to compare the length if(a.length()>b.lengtg()){ cout<<a; } else{ cout<<b; }
14th Dec 2017, 1:30 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar