Getting multiple values with scanf()

前端 未结 7 1136
孤街浪徒
孤街浪徒 2020-12-03 03:03

I am using scanf() to get a set of ints from the user. But I would like the user to supply all 4 ints at once instead of 4 different promps. I know I can get one value by do

相关标签:
7条回答
  • 2020-12-03 03:09

    Passable for getting multiple values with scanf()

    int r,m,v,i,e,k;
    
    scanf("%d%d%d%d%d%d",&r,&m,&v,&i,&e,&k);
    
    0 讨论(0)
  • 2020-12-03 03:13
    int a,b,c,d;
    if(scanf("%d %d %d %d",&a,&b,&c,&d) == 4) {
       //read the 4 integers
    } else {
       puts("Error. Please supply 4 integers");
    }
    
    0 讨论(0)
  • 2020-12-03 03:19

    Could do this, but then the user has to separate the numbers by a space:

    #include "stdio.h"
    
    int main()
    {
        int minx, x, y, z;
    
        printf("Enter four ints: ");
        scanf( "%i %i %i %i", &minx, &x, &y, &z);
    
        printf("You wrote: %i %i %i %i", minx, x, y, z);
    }
    
    0 讨论(0)
  • 2020-12-03 03:19

    Just to add, we can use array as well:

    int i, array[4];
    printf("Enter Four Ints: ");
    for(i=0; i<4; i++) {
        scanf("%d", &array[i]);
    }
    
    0 讨论(0)
  • 2020-12-03 03:26
    int a[1000] ;
    for(int i = 0 ; i <= 3 , i++)
    scanf("%d" , &a[i]) ;
    
    0 讨论(0)
  • 2020-12-03 03:28

    Yes.

    int minx, miny, maxx,maxy;
    do {
       printf("enter four integers: ");
    } while (scanf("%d %d %d %d", &minx, &miny, &maxx, &maxy)!=4);
    

    The loop is just to demonstrate that scanf returns the number of fields succesfully read (or EOF).

    0 讨论(0)
提交回复
热议问题