2

I have to write a program which takes an input which consists of the following space-separated values: int, long, char, float, and double, respectively and then print each of these values in a new line in the output.

When I'm writing the program using printf and scanf, it works perfectly fine.

#include<stdio.h>
int main()
{
    int a;
    long b;
    char c;
    float d;
    double e;
    scanf("%d %ld %c %f %lf", &a, &b, &c, &d, &e);
    printf("%d\n%ld\n%c\n%f\n%lf", a, b, c, d, e);
    return 0;
}

But when I'm using cin and cout, there's some problem happening. It happens when the number being entered for long is greater than LONG_MAX.

#include<iostream>
using namespace std;
int main()
{
    int intNumber;
    long longNumber;
    char character;
    float floatNumber;
    double doubleNumber;
    cin>>intNumber>>longNumber>>character>>floatNumber>>doubleNumber;                
    cout<<intNumber<<"\n"<<longNumber<<"\n"<<character<<"\n"<<floatNumber<<"\n"<<doubleNumber;
   return 0;
}

For example, if the input is 211916801 97592151379235457 p 19856.992 -5279235.721231465, the first program's output is

211916801
97592151379235457
p
19856.992
-5279235.721231465

But, the second program's out is

211916801
2147483647
╠
-1.07374e+08
-9.25596e+61

What exactly is happening here?

2
  • The long looks like it's being treated as an int. The char looks like an encoding issue. You're not having these problems with printf because the format specifiers in printf know what to do. Feb 15, 2019 at 18:03
  • I think the cin>> gave up after the out-of-range input for longNumber (assuming long is 32-bits for this compiler). Try initializing the variables to see which variables still have their initial values (and haven't been updated by cin>>) at the time of the cout<<.
    – Ian Abbott
    Feb 15, 2019 at 18:07

2 Answers 2

2

The istream::operator>> function calls the std::num_get::get function which calls the std::num_get::do_get function.

The do_get function does parsing in three stages, and for stage 3 (conversion and storage) there is this note:

If the conversion function results in a positive value too large to fit in the type of v, the most positive representable value is stored in v

[Note: v is the destination variable]

This explains what happens in with the value, and why you get 2147483647 for the long value. We can also from this say that you're on a system where the size of long is still 32 bits.

The list of notes for stage 3 then continues:

In any case, if the conversion function fails std::ios_base::failbit is assigned to err

And that explains what happens with the rest of the values. Once failbit is set you can't read more from the input stream until you clear the state.


Furthermore, if we go back to the operator>> reference we see that for C++98 and C++03

If extraction fails ... value is left unmodified and failbit is set.

And for C++11 and later

If extraction fails, zero is written to value

Since your variables after the failbit is set are not zero then we can deduce that you're building this with a pre-C++11 compiler. The values you print are the uninitialized contents which is indeterminate. And even reading indeterminate contents leads to undefined behavior.

2
  • In that case, how does scanf() works perfectly fine? Feb 16, 2019 at 17:32
  • @SaurabhPrakash Because the scanf function doesn't work like the C++ "input" operators. Feb 16, 2019 at 20:59
0

You inputed a long number is out of MAX_LONG = 2147483647. So that in this case it is overflow issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.