0

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 :-

enter image description here

  • 2
    Debugger. Excellent example to use a debugger. A debugger allows you to single step through your program, watching variables. – Thomas Matthews Jun 11 '21 at 14:43
  • 1
    _It is only taking input of string s1 and not taking input of string s2._ Are you sure? I would expect the opposite: `s1` is an empty string always and `s2` is the only which can be entered. The reason is simple. To input `choice`, you have to confirm with ENTER but the `cin >> choice;` will leave this in the input buffer. Then, if you call `getline(cin,s1);`, it's consumed immediately - leaving an empty string in `s1`. – Scheff's Cat Jun 11 '21 at 14:43
  • 1
    print messages to see where you're actually asking for the two strings. `cout << "Enter string one: "; getline(cin,s1); cout << "Enter string two: "; getline(cin,s2);` – stefan_aus_hannover Jun 11 '21 at 14:45
  • 1
    To fix this, you should investigate into [std::istream::ignore](https://en.cppreference.com/w/cpp/io/basic_istream/ignore). (And, the hint of @ThomasMatthews to debug this, is worth as well, of course.) – Scheff's Cat Jun 11 '21 at 14:45

1 Answers1

-1

Apparently using cin>> leaves out the '\n' from the input that is absorbed by the first getline() that, as @Scheff'sCat said, reads everything until '\n'. This means that the first getline() exits right away and shows only the second one.

You can try to use cin.ignore('\n').

Fabio R.
  • 393
  • 3
  • 15
  • _Why don't you just use cin instead of getline()_ Because the formatted input operators stop at the first occurrence of white space while `getline()` reads until delimiter (`\n` by default) or EOF. – Scheff's Cat Jun 11 '21 at 14:47
  • I read things wrong. my apologies. – stefan_aus_hannover Jun 11 '21 at 14:51
  • @steveo314 Ah, I see. The italic printed stuff was cited, the other my comment on this. ;-) – Scheff's Cat Jun 11 '21 at 14:52
  • Forgot what italics are for. I redacted my statement. – stefan_aus_hannover Jun 11 '21 at 14:54
  • @Scheff'sCat That's right, my bad. I think i actually found the answer, i'm editing it – Fabio R. Jun 11 '21 at 15:07
  • 1
    Note: If you use `ignore`'s delimiter parameter instead of just taking the default of EOF you must specify the count. Typically this is done with `cin.ignore(numeric_limits::max(), '\n')` to ignore everything that can possibly be in the stream, but this is often complete overkill. If you haven't found the newline in the first few hundred or thousand characters of user input something else has probably gone wrong. [Documentation link](https://en.cppreference.com/w/cpp/io/basic_istream/ignore) – user4581301 Jun 11 '21 at 16:05