Hi I am solving a question of the book C++ Primer by Stanely. The questions is as follows :-
Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger. Now, change the program to report whether the strings have the same length, and if not, report which is longer.
I have used a variable choice to switch between the program i.e whether to check strings are equal or not. Or to check whether the strings have same length or not.
#include<iostream>
using namespace std;
int main(){
char choice;
cout<<"Please enter choice"<<endl<<"For Larger press (L) and for longer press (l) "<<endl;
cin>>choice;
string s1, s2 ;
getline(cin,s1);
getline(cin,s2);
if(choice=='L'){
if(s1!=s2){
if(s1>s2) {
cout << "string which is larger is : " <<s1<<endl;
}
else{
cout<<"string which is larger is : " <<s2<<endl;
}
}
else{
cout<<"Both strings are equal "<<endl ;
}
}
else if (choice == 'l'){
if(s1.size() != s2.size()){
if(s2.size()> s1.size()){
cout<<"Longer string : "<<s2<<endl;
}
else {
cout<<"Longer string : " << s1<<endl;
}
}
else {
cout<<"Both strings have same length" <<endl;
}
}
else{
cerr<<"wrong input!! "<<endl;
return -1;
}
return 0;
}
but when I'm compiling the program, It is only taking input of string s1 and not taking input of string s2.
The output is as follows :-