Reputation: 549
I am trying to read in integers through stdin using scanf however when I try to run my code it wants me to type in an integer instead of reading the line.
int x = 0;
int y = 0;
scanf("%d", &x);
while (x != '\n') {
y += x;
scanf("%d", &x);
}
Why won't my code read what's in stdin?
Upvotes: 0
Views: 51
Reputation: 25129
You don't say how it's crashing, but these lines:
scanf("%lld", &x);
while (x != '\n') {
...
}
Will read a long integer into x
, then execute the while
loop unless the number you entered was 10 (ascii for \n
). I'm pretty sure that's not what you wanted, but then it is hard to know what you did want.
Were you trying to loop until no number was input? If so perhaps check the return value from scanf()
.
Note each call to scanf()
is going to read a line.
Upvotes: 1