f******y 发帖数: 2971 | 1 For the following bullet on FAQ, I did not see any problem with method 1.
FAQ said it is problematic. And it did not explain too much. Anyone can
explain it better?
[15.6] Why is my program ignoring my input request after the first iteration?
Because the numerical extractor leaves non-digits behind in the input buffer.
If your code looks like this:
char name[1000];
int age;
for (;;) {
std::cout << "Name: ";
std::cin >> name;
std::cout << "Age: ";
std::cin >> age;
}
What you really wa |
t****t 发帖数: 6806 | 2 what do you mean it did not explain too much? it explains exactly what
happens.
iteration?
buffer.
【在 f******y 的大作中提到】 : For the following bullet on FAQ, I did not see any problem with method 1. : FAQ said it is problematic. And it did not explain too much. Anyone can : explain it better? : [15.6] Why is my program ignoring my input request after the first iteration? : Because the numerical extractor leaves non-digits behind in the input buffer. : If your code looks like this: : char name[1000]; : int age; : for (;;) { : std::cout << "Name: ";
|
f******y 发帖数: 2971 | 3 So you mean the input was read as a string, and there is a '\n' in this
string. When compiler parses the content in the stream, only the numerical
part is read, the '\n' is left there. We need explicitly call ignore
function to ignore the '\n'.
Am I right?
【在 t****t 的大作中提到】 : what do you mean it did not explain too much? it explains exactly what : happens. : : iteration? : buffer.
|
t****t 发帖数: 6806 | 4 yes that's what the FAQ meant (I didn't "mean" anyting since I didn't say
anything). see, you understand exactly what happens, what's all the complain
about? you just have to read carefully.
【在 f******y 的大作中提到】 : So you mean the input was read as a string, and there is a '\n' in this : string. When compiler parses the content in the stream, only the numerical : part is read, the '\n' is left there. We need explicitly call ignore : function to ignore the '\n'. : Am I right?
|
f******y 发帖数: 2971 | 5 Thanks for the confirmation. Yes, I need read it carefully.
say
complain
【在 t****t 的大作中提到】 : yes that's what the FAQ meant (I didn't "mean" anyting since I didn't say : anything). see, you understand exactly what happens, what's all the complain : about? you just have to read carefully.
|
h**k 发帖数: 3368 | 6 这个行为是依赖于编译器的么?
我用g++编译执行了第一段代码,完全正确,不需要改进。
这是为什么?
iteration?
buffer.
【在 f******y 的大作中提到】 : For the following bullet on FAQ, I did not see any problem with method 1. : FAQ said it is problematic. And it did not explain too much. Anyone can : explain it better? : [15.6] Why is my program ignoring my input request after the first iteration? : Because the numerical extractor leaves non-digits behind in the input buffer. : If your code looks like this: : char name[1000]; : int age; : for (;;) { : std::cout << "Name: ";
|
t****t 发帖数: 6806 | 7 try add some garbage after your age input.
【在 h**k 的大作中提到】 : 这个行为是依赖于编译器的么? : 我用g++编译执行了第一段代码,完全正确,不需要改进。 : 这是为什么? : : iteration? : buffer.
|