0

why wont this work

#include <iostream> using namespace std; int main() { try { string name; cin >> name; if (name >=20 && name >=4) { cout << "Valid" << endl; throw 99; } } catch (int x) { cout<<"Invalid"<<endl; } return 0; }

8th Dec 2021, 2:15 PM
Mando
Mando - avatar
4 Respostas
+ 2
Mando Name is string and 20 is a number then how you can compare? get size of name then compare. if size >= 20 then how it can be >=4 size should be <=20 && >= 4
8th Dec 2021, 2:35 PM
A͢J
A͢J - avatar
+ 1
AJ then why doesnt this work then #include <iostream> using namespace std; int main() { try { int name; cin >> name; if (name<=20 && name >= 4) { cout << "Valid" << endl; } else cout << "Invalid" << endl; } return 0; }
9th Dec 2021, 3:35 PM
Mando
Mando - avatar
0
Mando Name is string not int. Mando can't be 4 or 20 it's a string Mando >= 4 //wrong comparison 5 >= 4 //right comparison First code is right but you need to compare size of string with 20 and 4 Notes: Read my first answer again
9th Dec 2021, 6:13 PM
A͢J
A͢J - avatar
0
AJ #include <iostream> using namespace std; int main() { string name; cin >> name; try { string str = name ; str.size(); if (str <=20 && >= 4 ){ cout << "Valid" << endl; } else cout << "Invalid"<< endl; } catch(int x) { } return 0; } I dont understand i followed the steps and it still doesnt work
10th Dec 2021, 3:07 PM
Mando
Mando - avatar