You are not logged in. Please login at to post your questions!


unable to get input the proper way.

In c++, I want to input a string after I input a number. This is my code. Why string s doesn't get any input? What is the correct way of doing it?

asked 13 Feb '17, 22:17

ayush933's gravatar image

accept rate: 0%

The problem is that you are not flushing the newline ('\n') character after cin>> statement.

so you must need to flush the newline character out of the buffer in between. You can do it by using cin.ignore(). This happens because the >> operator leaves a newline \n character in the input buffer which which be taken by getline() prior to reading your next input.

Click here to see my code.


answered 13 Feb '17, 23:21

bansal1232's gravatar image

accept rate: 16%

Why doesn't this happen when we try to input 2 int which are on different lines? This works but the previous one doesn't.

(13 Feb '17, 23:28) ayush9334★
  • First of all you have to understand how both cin>> and getline() works.

cin>> starts working by first eliminating all white spaces and newline character. That's why input like in one line

   45 98 65

will be taken as 3 input. Why?, because cin>> will automatically terminate and stop taking input after getting any white space or newline character.

  • getline works differently. It takes any character at starting whether it is white space or newline. That's why i had to include cin.ignore() to eliminate newline character. Another property of getline is it will only terminate after getting newline('\n') character.

answered 13 Feb '17, 23:51

bansal1232's gravatar image

accept rate: 16%

edited 13 Feb '17, 23:52


Exactly what I had to say!! Well explained! :)

(13 Feb '17, 23:54) vijju123 ♦♦5★

Nice Explanation. Thanks.

(14 Feb '17, 02:01) ayush9334★

Correct way-

    #include <iostream>
using namespace std;
int main() 
    int b;
    string s;
    return 0;

I suspect that whatever you are using to get the string input, is getting the null character/end line character and assigning it to string. Its pretty common error programmers do.

EDIT 1 - getline usually reads the entire line, including the new line character etc. So it accepts new line character as a valid input, which most other inputs methods/keywords (idk what to call them atm XD) usually don't.

PS, I think you can find something good here-

stack overflow stack overflow 2


answered 13 Feb '17, 22:38

vijju123's gravatar image

5★vijju123 ♦♦
accept rate: 18%

edited 13 Feb '17, 23:38

Even if I try it this way I only get the first word of the string as input.

(13 Feb '17, 23:30) ayush9334★

Oh, so you want to input a sentence? It wont work for that. Try Bansal's method for that. I saw no indication for sentence (strings with white spaces have different input that strings without white spaces)

using cin.ignore() is the way to go bud, cause cin>>s will stop at the first whitespace it encounters

(13 Feb '17, 23:33) vijju123 ♦♦5★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 13 Feb '17, 22:17

question was seen: 593 times

last updated: 26 Dec '17, 10:15