A little problem about cin

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

Moderator: Board moderators

Post Reply
Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

A little problem about cin

Post by Rene » Thu Jul 24, 2003 5:37 pm

I am writing a program about clock these days.
but there are a little problem about input, then i have writen a test program.And i have found a strange thing.
This is my code:
[cpp]
#include <iostream.h>

void main()
{
int a;
while (cin >> a)
cout << a << endl;
}
[/cpp]
  • input:
    15
    00
    01
    02
    05
    07
    08
    09
    10
  • output:
    15
    0
    1
    2
    5
    7
    0
    8
    0
    9
    10
The question is:why input 01,02,05 and 07(in fact all input small than 08) a = 1,2,5,7.... But when input is bigger than 07,a = 0,8 and 0,9.
I have input only one number,why a have two number?
Please help me,Thank you.
Le roi c'est loi,le loi c'est roi

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Thu Jul 24, 2003 6:20 pm

Hi
Wich compiler do you use ? Gcc 3.2 ? 2.96 ? TC ? VC++ ? on wich os ? Linux ? Windows ?
Have you tried to include <iostream> and not <iostream.h> (wich is older and now provided for backward compatibility, though I don't think it changes much) ?

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

Post by Rene » Fri Jul 25, 2003 6:06 am

I use TC and VC++. They have the same problem.
My OS is Windows.
can i include <iostream>?
i always use iostream.h.
Thank you.
Le roi c'est loi,le loi c'est roi

Viktoras Jucikas
New poster
Posts: 22
Joined: Sun Oct 20, 2002 6:41 pm
Location: Lithuania
Contact:

Post by Viktoras Jucikas » Fri Jul 25, 2003 8:49 am

can i include <iostream>?
You should include iostream, not iostream.h, as .h are provided only for backwards compatibility (for compilers which do not support namespaces, as everything in iostream resides in std namespace). For old c inludes like math.h you should use cmath.
The question is:why input 01,02,05 and 07(in fact all input small than 0 a = 1,2,5,7.... But when input is bigger than 07,a = 0,8 and 0,9.
I have input only one number,why a have two number?
The problem is that any number with leading '0' is treated as an octal number. Thus 08 and 09 are not valid numbers... Stream routines read until they find 8 or 9 after leading 0, then stop and output number they have read (this is 0 in your case). After that they read further and find perfectly valid decimal numbers 8 or 9.

So in this case you're probably better off with reading char[3]...

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Fri Jul 25, 2003 4:01 pm

I don't know much in C++ (not yet, I'm learning too), but if reading "09" from input is really a pain in the neck, you still can use scanf() function, wich is originally used in C and is replaced in C++ with stream management, but can still be used.
I personnally chose this for ACM problems, because I've been using C for 2 years and am only coming to C++, so I know printf and scanf kinda well, and much better than I know C++ streams.

Rene
New poster
Posts: 13
Joined: Mon May 05, 2003 4:40 am
Location: Shanghai,China

Post by Rene » Fri Jul 25, 2003 6:11 pm

yes,i can use scanf(). but i don't know much about the function in C. In UVA, the most input is terminated by End Of File. when i use stream, cin will throw an exception when it read EOF, i use
[cpp] while (cin >> a)[/cpp] to find the End Of File.
If i use scanf(), how should i write
[c]while (scanf ("%d",a) != EOF)[/c]
is it right?
Le roi c'est loi,le loi c'est roi

Julien Cornebise
Experienced poster
Posts: 145
Joined: Sat Feb 23, 2002 2:00 am
Location: Paris, France
Contact:

Post by Julien Cornebise » Fri Jul 25, 2003 10:45 pm

Well, the thing that works the most is to use the fact that scanf returns the number or arguments correctly read :
[c]
int a, b;
scanf("%d %d", &a, &b);
[/c]
will return 2 if a AND b has been read, separated by a space.
So the easer thing to do is to
[c]while( scanf("%d", &a) == 1) [/c]
And that should be OK (that's what I've done so far, from the beginning).

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Jul 26, 2003 3:37 am

Let T be a type. cin>>T, if defined, reads the biggest continuous string of characters and makes it to fit T type. So when T is a char, you read only one character. When T is a number type, you read the whole number.
And a string is continuous if it doesn't contain any spaces or end of file/line characters. Such characters are not removed from the stream, except for the case with T being a char(or array of chars I suppose - I don't know as I use string template class instead :-) )

Post Reply

Return to “C++”