Reading in from Stdin in C++

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
alanchua
New poster
Posts: 1
Joined: Tue Jul 17, 2012 9:43 pm

Reading in from Stdin in C++

Post by alanchua » Tue Jul 17, 2012 9:59 pm

Hi all

I am new to the competition contests. I have some question that I hope that I can get some advice.

If a question never mention the number of lines that are expected to read from stdin. I will use a while loop to getline from stdin. The question is

1. Under what condition will I need to check so that I can terminate out of the loop since the the expected number of lines or condition might not be stated in the question.

Hope my question is clear.:) Please forgive me if this is begin discussed before cos I am totally new in contests.


Some question will be straight forward as they might tell you that the first line of the input will determine the number of line to be expected. eg

3 --> This number determine the number of lines that will be expected.
This is line number 1
This is line number 2
This is line number 3

Regards

Alan Chua

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Reading in from Stdin in C++

Post by brianfry713 » Tue Jul 17, 2012 11:25 pm

There is no standard input format for all problems. Sometimes you can read input an integer or double or string at a time, other problems require you to read line by line.
Check input and AC output for thousands of problems on uDebug!

Zyaad Jaunnoo
Experienced poster
Posts: 122
Joined: Tue Apr 16, 2002 10:07 am

Re: Reading in from Stdin in C++

Post by Zyaad Jaunnoo » Wed Oct 29, 2014 6:23 pm

If we are dealing with strings, I use:

Code: Select all

string s;

while(getline(cin, s))
{

}
If we are dealing with integers, I use:

Code: Select all

int n;

while (cin >> n)
{

}

Post Reply

Return to “C++”