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?

0

2 Answers 2

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; } 
Sign up to request clarification or add additional context in comments.

Comments

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; } 

6 Comments

i know we could have used geline() in both the places, but i wanted to know why getline() was not taking input after cin>>
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" , pediaa.com/what-is-the-difference-between-getline-and-cin
@hari why doesn't my answer reply to your question about getline() not taking input after cin?
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.
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 " quora.com/… .
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.