Home > Software design >  how to input several input in c
how to input several input in c

Time:10-14

so i started to learn some basic coding and i am beginner. now i am trying to code about leap year program. so how can i type several input without stoping the program.

the input is : 2000 2002 2004 4 1999

the output is : leap nonleap leap leap nonleap

but my program only can do 1 input at the time , this is my code

#include <stdio.h>  
  
int main()   
{  
     
   int y;  
     
   scanf("%d", &y);  
  
   if (y % 400 == 0) {  
      printf("leap\n");  
   }  
   else if (y % 100 == 0) {  
      printf("nonleap\n");  
   }  
   else if (y % 4 == 0) {  
      printf("leap\n");  
   }  
   else {  
      printf("nonleap\n", y);  
   }  

   return 0;  
} 

so when i type the input e.x 2000 and it print leap but the program stop. can someone help me so the program not stop until there is no input left.

CodePudding user response:

Start reading about the C programming basics. This can be achieved by using loops in C. Read about loops.

Below, I have added a while loop and an exit condition to your above code.

#include <stdio.h>
#include <string.h>

int main()
{
    while (1) {
        int y;
        scanf("%d", &y);

        if (y % 400 == 0) {
            printf("leap\n");
        }
        else if (y % 100 == 0) {
            printf("nonleap\n");
        }
        else if (y % 4 == 0) {
            printf("leap\n");
        }
        else {
            printf("nonleap\n");
        }

        char buf[8];
        printf("continue(y/n)?");
        scanf("%s", buf);
        if (strcmp(buf, "n") == 0) {
            break;
        }
    }
    return 0;
}

CodePudding user response:

You can use a loop like this :

#include <stdio.h>

int main(void) {
    int y;
    while (scanf("%d", &y) == 1) {
        if (y % 400 == 0) {
            printf("leap\n");
        }
        else if (y % 100 == 0) {
            printf("nonleap\n");
        }
        else if (y % 4 == 0) {
            printf("leap\n");
        }
        else {
            printf("nonleap\n");
        }
    }
    return 0;
}

Let me explain to you why it works : when you enter an input, it first goes into what we call a "buffer", the content of this buffer is tested by the scanf function, if it matches the expected type (%d in your case), it puts this content into the adress you specified (&y in your case) and returns the number of well read values (1 in your case).

If it doesn't matches this will still return the number of well read values (0 in your case) BUT will keep the last not well read value into the buffer.

(This part is a little more complicated than what you are asking for so you can ignore it :

That means that if reading a bad input doesn't stops your program and that you have another scanf in your code, the value computed by the second scanf will be the one that has not been well read in the first scanf.)

With the code I wrote, if you write multiple integers (separated with space) as input, it will compute the first value through scanf, return 1 if the value is an integer and 0 if not.

When the first value has been computed by scanf AND the rest of the loop code, this will print "leap" or "non leap" then automatically read the next integer.

This way, if you enter a wrong value, loop will stop.

I hope everything is clear, ask me to re explain that if you want !

PS : You can hit CTRL D if you want to stop reading inputs, this will stop your program

  •  Tags:  
  • c
  • Related