Connecting Tech Pros Worldwide Help | Site Map

Is input/output stream buffer behaviour random acc. to C++ standard?

qazmlp1209@rediffmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
FAQ at
"http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6"
states that the following program will not work properly:
---------------
int main()
{
char name[1000];
int age;

for (;;)
{
std::cout<< "Name: ";
std::cin >> name;
std::cout<< "Age: ";
std::cin >> age;
}
}
---------------

But,if I run the above program on my machine, it works without any
problems. So, does it mean that, keeping the 'non-digit character
within buffer' problem is a random behaviour exhibited by some/all
systems? I could understand that, 'ignore()' of non-digit characters
that are left behind, is safer. But, I would like to know what exactly
is the case without that.

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Is input/output stream buffer behaviour random acc. to C++ standard?


qazmlp1209@rediffmail.com wrote:[color=blue]
> FAQ at
> "http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6"
> states that the following program will not work properly:
> ---------------
> int main()
> {
> char name[1000];
> int age;
>
> for (;;)
> {
> std::cout<< "Name: ";
> std::cin >> name;
> std::cout<< "Age: ";
> std::cin >> age;
> }
> }
> ---------------
>
> But,if I run the above program on my machine, it works without any
> problems.[/color]

What input are you giving it?
[color=blue]
> So, does it mean that, keeping the 'non-digit character
> within buffer' problem is a random behaviour exhibited by some/all
> systems?[/color]

No.
[color=blue]
> I could understand that, 'ignore()' of non-digit characters
> that are left behind, is safer. But, I would like to know what exactly
> is the case without that.[/color]

Try giving it some garbage instead of an integer when 'age' is requested.

V


Larry I Smith
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Is input/output stream buffer behaviour random acc. to C++ standard?


qazmlp1209@rediffmail.com wrote:[color=blue]
> FAQ at
> "http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6"
> states that the following program will not work properly:
> ---------------
> int main()
> {
> char name[1000];
> int age;
>
> for (;;)
> {
> std::cout<< "Name: ";
> std::cin >> name;
> std::cout<< "Age: ";
> std::cin >> age;
> }
> }
> ---------------
>
> But,if I run the above program on my machine, it works without any
> problems. So, does it mean that, keeping the 'non-digit character
> within buffer' problem is a random behaviour exhibited by some/all
> systems? I could understand that, 'ignore()' of non-digit characters
> that are left behind, is safer. But, I would like to know what exactly
> is the case without that.
>[/color]

Does it work correctly when you enter "abc" or "9 xx" in response
to the "Age:" prompt?

Regards,
Larry
Closed Thread