Home > Blockchain >  The for loop in C is not executed
The for loop in C is not executed

Time:10-24

The problem is my code doesn't execute the for loop. It's just taking one input and printing it.

#include<stdio.h>
        
int main(){
  int A[100], B[100], C[100], D[100];
  int r[100];
        
            
  for(int i = 0; i < 3; i  )
  {
      scanf("(%d %d)x(%d-%d)", &A[i], &B[i], &C[i], &D[i]);
      r[i] = (A[i] B[i])*(C[i]-D[i]);
  }
            
  for(int k = 0; k < 3; k  )
  {
      printf("%d", r[k]);
  }
        
  return 0;
}

CodePudding user response:

Assuming you input expressions are separated by a newline, when the second call to scanf is made, the first unread character is a newline rather than a left parenthesis. To make scanf skip leading whitespace characters, start the format string with a space, i.e. " (%d %d)x(%d-%d)".

CodePudding user response:

First a fall this is the wrong way to use scanf.

use the below code:-

    #include<stdio.h>
    int main(){
    
    int A[100], B[100], C[100], D[100];
    int r[100];

    
    for(int i = 0; i < 3; i  )
    {
         scanf("%d %d %d %d", &A[i], &B[i], &C[i], &D[i]);
         r[i] = (A[i] B[i])*(C[i]-D[i]);
    }
    
    for(int k = 0; k < 3; k  )
        printf("%d", r[k]);
    return 0;
}

CodePudding user response:

i fixed it with add getchar after the line r[i] = (A[i] B[i])*(C[i]-D[i]);

  •  Tags:  
  • c
  • Related