Reputation: 113
I have the following code, but why doesn't the second while loop run? Any changes to make it run?
#include<stdio.h>
int main() {
int a,b,c,d,e,f;
while(scanf("%d,%d,%d",&a,&b,&c)==3){
printf("ok\n");
}
while(scanf("%d,%d,%d",&d,&e,&f)==3){
printf("OK\n");
}
return 0;
}
My input is
1,2,3
1,5,7,4,8,7
7,8,9,...
Upvotes: 0
Views: 152
Reputation: 108988
Let's say you input "1,5,7,4,8,7<ENTER>" for the scanf()
in the 1st while
.
The scanf("%d,%d,%d", &a, &b, &c);
reads 1 into a
, 5 into b
, 7 into c
and returns 3
keeping ",4,8,7<ENTER>"
in the input buffer.
In the 2nd time through the loop, scanf finds the comma and returns 0
which terminates the while
.
Immediately after that, in the 2nd while
, the 2nd scanf()
attempts to convert ",4,8,7<ENTER>"
to an integer and fails promptly returning 0
and terminating the while
.
Upvotes: 1