0

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,...
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Please specify what input are you providing – ikegami Mar 13 '21 at 11:17
  • 1,2,3(press enter) 1,5,7, stops at this point but needs to take the input 4,8,7 (press enter) 7,8,9,.. – hrithik mahesh Mar 13 '21 at 11:19
  • 1
    the second loop won't run until the first one is running. try to enter less numbers and see that the first loop will stop and the second will run. – MarcoLucidi Mar 13 '21 at 11:22
  • when it enter a,b,c it keeps running but when I enter a,b,c, it will stop and second while should run but it's not running – hrithik mahesh Mar 13 '21 at 11:24
  • @user3121023 how to get over that issue? – hrithik mahesh Mar 13 '21 at 11:25
  • @hrithikmahesh Please initialize the variables before using them (e.g `int a = 0; int b = 0;` (...) – William Martens Mar 13 '21 at 11:26
  • 1
    Re "*when I enter a,b,c, it will stop and second while should run but it's not running*", That's because you have `,4,8,7` but your pattern expects `4,8,7` – ikegami Mar 13 '21 at 11:28
  • @William Martens, They are initializing them with `scanf` – ikegami Mar 13 '21 at 11:29
  • @ikegami is there any way to remove "," from the input stream before entering to second while loop? – hrithik mahesh Mar 13 '21 at 11:30
  • @ikegami Well, it is good practice to initialize variables, or else it will result in undefined behavior. Check here https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value https://stackoverflow.com/questions/7786921/why-we-must-initialize-a-variable-before-using-it Even if they are, always do initialize them to zero. (int his case int) (Read your below comment. I can just provide advice.. please at least check the last link) – William Martens Mar 13 '21 at 11:30
  • @Willam Martens. Again, They are already being initialized. There's absolutely no point in doing `int a = 0; a = 4;` as you suggest. – ikegami Mar 13 '21 at 11:31
  • @ikegami https://stackoverflow.com/questions/19747029/why-is-used-before-a-variable There **is** a point of doing so. Please read the link. (Specifically, at the accepted answer, at the "k" part) – William Martens Mar 13 '21 at 11:38
  • @William Martens, No there isn't, but there is a bug that needs to be fixed. Did you not read the answer to the very question you linked? The problem is that the value retuned by `scanf` was ignored. This isn't a problem for the OP. They made sure they were initialized. – ikegami Mar 13 '21 at 11:40

1 Answers1

1

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.

pmg
  • 106,608
  • 13
  • 126
  • 198