0
int main() {
   string s1,s2;
   cout<<"1. "<<endl;     
   cin>>s1;                      //to accept 1st string
   cout<<s1<<endl;  
   cout<<"2. "<<endl;
   getline(cin,s2);             //to accept 2nd string
   cout<<s2<<endl;
}

Here in the above code after accepting the 1st string it is not asking for the 2nd string: the program is getting terminated after taking the 1st input without waiting for the 2nd.

Could anyone kindly explain what the reason of such behavior is? And why is it not waiting for getline(cin,s2) for taking user input?

John Smith
  • 1,027
  • 15
  • 31
HARI
  • 1

2 Answers2

1

That is happening because getline reads \n at the end of your first line. So it read and printed "\n" while you think it expects a new line. I suggest use getline twice (so firstly it reads \n, then your second line). And please, use std::, don't use using namespace std, and use spaces as any normal codestyling sais.

int main() {
    std::string s1, s2;
    std::cout << "1. " << std::endl;     
    std::cin >> s1;                      //to accept 1st string
    std::cout << s1 << std::endl;  
    std::cout << "2. " << std::endl;
    std::getline(std::cin, s2);             //to accept \n
    std::getline(std::cin, s2);             //to accept 2nd string
    std::cout << s2 << std::endl;
}
Mark Tiukov
  • 82
  • 1
  • 11
0

Here the error is there you need misunderstand the return type . Here you have used int for main method . So there you need return type. If you have used void for main method you haven't need a return type . You can use modify the code as below .

This is for printing single string

 #include <iostream>
 using namespace std;

 int main(){ 

     string s1;
     cout<<" Enter the first string :"
     getline(cin,s1);

     cout<<"The input string is"<<s1 <<endl;

     return 0; 

 }

You can modify the code as below to output the two strings as below

 #include <iostream>
 using namespace std;

 int main(){ 

     string s1,s2;
     cout<<" Enter the First string :"
     getline(cin,s1);

     cout<<"The First string is"<<s1 <<endl;

     cout<<" Enter the Second string :"
     getline(cin,s2);

     cout<<"The Second string is"<<s2 <<endl;

     return 0; 

 }
Kaviranga
  • 576
  • 2
  • 10
  • 24
  • i know we could have used geline() in both the places, but i wanted to know why getline() was not taking input after cin>> – HARI Apr 07 '20 at 11:26
  • Extracted answer "The main difference between getline and cin is that getline is a standard library function in the string header file while cin is an instance of istream class. In breif, getline is a function while cin is an object.  Usually, the common practice is to use cin instead of getline" , https://pediaa.com/what-is-the-difference-between-getline-and-cin/ – Kaviranga Apr 07 '20 at 11:57
  • @hari why doesn't my answer reply to your question about getline() not taking input after cin? – Mark Tiukov Apr 07 '20 at 14:13
  • BTW, this answer is not correct because in C++ we don't need the return in main function. And it has nothing to do with reading/printing strings. – Mark Tiukov Apr 07 '20 at 20:50
  • Contrasting this answer on quora "If you write int main(), it means that main() will return some integer,you can return anything,but as a convension we use 0,which means success.if main() fails to do its intended work we can return some non-zero number " https://www.quora.com/Why-do-we-use-a-return-0-at-the-end-of-a-main-function/ . – Kaviranga Apr 07 '20 at 23:47
  • As an aside: noticed you edited [this question](https://stackoverflow.com/questions/62128170/how-to-loop-an-element-of-java-swing) and embedded the image of the code. Don't do that. Images of text are not searchable and not welcome. Better to encourage the OP to post the actual text. Including the image just encourages them to do it again. (I re-edited the question to remove it, but please take note for future edits.) – Andrew Thompson Jun 02 '20 at 06:05