You are not logged in. Please login at www.codechef.com 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

4★ayush933
975
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.

link

answered 13 Feb '17, 23:21

bansal1232's gravatar image

5★bansal1232
2.8k1419
accept rate: 16%

Why doesn't this happen when we try to input 2 int which are on different lines? http://ideone.com/464EAy 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.
link

answered 13 Feb '17, 23:51

bansal1232's gravatar image

5★bansal1232
2.8k1419
accept rate: 16%

edited 13 Feb '17, 23:52

1

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>
#include<string>
using namespace std;
int main() 
{
    int b;
    string s;
    cin>>b;
    cin>>s;
    cout<<b<<endl;
    cout<<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

link

answered 13 Feb '17, 22:38

vijju123's gravatar image

5★vijju123 ♦♦
15.5k12066
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
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×170

question asked: 13 Feb '17, 22:17

question was seen: 593 times

last updated: 26 Dec '17, 10:15