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

×

What is the difference in behavior between cin and getchar?

While working on a problem on UVa Online Judge, I received "Accepted" for my program which started like so:

int main() {
    int i, j;
    while(cin >> i >> j) {

However, when I replaced that with this:

void read(int &n) {
    n = 0;
    int sign = 1;
    char c = getchar_unlocked();
    while(c<'0' || c>'9') {
        if(c == '-') sign = -1;
        c = getchar_unlocked();
    }
    while(c>='0' && c<='9') {
        n = n*10+c-'0';
        c = getchar_unlocked();
    }
    n*=sign;
}
int main() {
    int i, j;
    while(1) {
        read(i); read(j);

I get TLE. The function read() is something I found here on codechef, and I believe it is used by many for fast input. However since I am new to C++ I have simply copied it without understanding the difference between cin and getchar_unlocked. I also do not understand how UVa inputs to my program. I know that my second piece of code contains an infinite loop, but it seems that the first example also loops indefinitely, so I assumed that UVa did not care. What is the difference?

asked 06 Aug '14, 23:24

ubeatlenine's gravatar image

2★ubeatlenine
611
accept rate: 0%


There is a big difference between the two loops. In the first one, you use cin, which will return a null pointer if there are no more values in the file. The second while will run forever since "1" will always be true, no matter what you have in the file. get_char will not cause the program to stop, even if all the characters from the file are "consumed".

For "cin" read this: http://stackoverflow.com/questions/6791520/if-cin-x-why-can-you-use-that-condition

link

answered 06 Aug '14, 23:58

alexvaleanu's gravatar image

4★alexvaleanu
5591312
accept rate: 7%

This makes sense! Thank you for your explanation. So what does UVa send to my program to terminate it? Is it a random invalid character, some special character, or something else entirely?

(07 Aug '14, 05:06) ubeatlenine2★

AFAIK your first piece of code does not run indefinitely. If I am getting this right, the problem you are solving has an input format such that it requires even number of integers. So "while (cin >> i >> j)" will execute until the integers are provided by UVa, while your second piece of code will run even if there is no input from the online judge (ie, after all integers have been provided).

(07 Aug '14, 10:38) nisargshah953★
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:

×68
×39
×19

question asked: 06 Aug '14, 23:24

question was seen: 4,933 times

last updated: 07 Aug '14, 10:38