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

Q

qazmlp1209

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.
 
V

Victor Bazarov

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;
}
}

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

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.

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

V
 
L

Larry I Smith

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.

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

Regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top