How to get multiple values ​​at a time? (Uncertainty)

How can I get a few values ​​typed from the keyboard (integer type and a lot of uncertainty)?

I need to write a program that allows users to enter any number, Each number is separated by a space, and when the user presses Enter, the Number was placed in each of the Array variables.

For instance,

input number: 1 2 8 9 (Enter), if you enter the fourth (digits), it will create four variables to get this value.

number [0] = 1, number [1] = 2, number [2] = 8, number [3] = 9.

input number: 3 4 7 (Enter), if you enter the third (digits), he will need to create three variables to get this value.

number [0] = 3, number [1] = 4, number [2] = 7.

I tried to use the scan function f, but it does not work,

If you have any good advice, please tell me.

#include <stdio.h>
#include <windows.h>

int n, number[10];

main()
{
    printf("Enter Number of integer : ");
    scanf("%d", &n);
    if (n == 1) {
        printf("Enter integer : ");
        scanf("%d", &number[0]);
    } else if (n == 2) {
        printf("Enter integer : ");
        scanf("%d %d", &number[0], &number[1]);
    } else if (n == 3) {
        printf("Enter integer : ");
        scanf("%d %d %d", &number[0], &number[1], &number[2]);
    }
    system("pause");
}
+4
source share
4 answers

Do you think it is using a combination scanf(), fgets()and strtok()?

For example, you can use the scanf()number of expected inputs for a simple query. Then use the loop around `strtok () to enter user input:

int main()
{
    char line[1024];
    char *token = {0};
    char delim[]={" \r\n\t"};
    long telPos = 0;
    int count, i;
    int *array = {0};

    printf("Enter Number of integers, <return> then line of integers : \n");
    scanf("%d",&count);

    getchar();//eat the newline (from <return>)

    while (fgets(line, 1024, stdin) != NULL)
    {
        array = malloc(sizeof(int)*count);
        token = strtok(line, delim);
        i = -1;
        while(token)
        {
            if((count - 1) > i++)
            {
                array[i] = atoi(token);
                token = strtok(NULL, delim);
            }
            else
            {
                for(i=0;i<count;i++) printf("value %d: %d\n", i, array[i]);
            }
        }
        //do something here with array, then free it.
        free(array);
    }

    return 0;

}
+2
source

Take a look at the scanf documentation . You can use the return value of a function to determine if an integer was read. that way you can do something like

while (scanf("%d") > 0) {
  // do stuff
}
+1
source

, , , . . . :

#include <stdio.h>

#define MAXI 10

int main (void) {

    int i = 0;
    int c = 0;
    int n = 0;
    int numbers[MAXI] = {0};

    printf ("\n Enter a number: ");
    while ((c = getchar()) != EOF && c != '\n')
    {
        if (c >= '0' && c <= '9')
        {
            numbers[n] = c - '0';
            n++;
            if (n == MAXI)
                break;
        }
    }

    printf ("\n The numbers entered are: ");
    for (i = 0; i < n; i++)
        printf ("%d", numbers[i]);
    printf ("\n\n");

    return 0;
}

<strong> Examples

$ ./bin/scanf_multi_ints <<<$( printf "01234567890\n" )

 Enter a number:
 The numbers entered are: 0123456789

$ ./bin/scanf_multi_ints <<<$( printf "0 1 2 3 4 5 6 7 8 9 0\n" )

 Enter a number:
 The numbers entered are: 0123456789

$ ./bin/scanf_multi_ints

 Enter a number: 01234567890

 The numbers entered are: 0123456789

$ ./bin/scanf_multi_ints

 Enter a number: 01abc234dog5 6 and 7 more 8 dogs 90

 The numbers entered are: 0123456789

Here the numbers can be entered in any way. With spaces in between. Without. With missing letters or words. You decide. Converting from character to number is a simple subtraction of decimal 48(0x30 hex).

+1
source

Use fgets()better. However, a preliminary reading of leading spaces before scanf("%d", &some_int)may also work. Find '\n'.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

// Scan & toss all white-space.  Return 1 on \n or EOF, else return 0
static bool EatWhiteSpaceUntilEOL(FILE *inf) {
  int ch;
  do {
    ch = fgetc(inf);
    if (ch == '\n' || ch == EOF)
      return true;
  } while (isspace(ch));
  ungetc(ch, inf);
  return false;
}

// Return number of `int` read.  Set *eol true if \n or EOF encountered.  
size_t Read_int(FILE *inf, int *dest, size_t n, bool *eol) {
  for (size_t i = 0; i < n; i++) {
    *eol = EatWhiteSpaceUntilEOL(inf);
    if (*eol) {
      return i;
    }
    if (fscanf(inf, "%d", &dest[i]) != 1) {
      return i;
    }
  }
  *eol = EatWhiteSpaceUntilEOL(inf);
  return n;

}

int main(void) {
  int n;
  printf("Enter maximum number of `int` to read: ");
  bool eol;
  if (Read_int(stdin, &n, 1, &eol) != 1 || !eol) {
    return EXIT_FAILURE;
  }

  int *a = calloc(n, sizeof *a);
  size_t count = Read_int(stdin, a, n, &eol);
  printf("Count:%zu First:%d Last:%i  Done:%s\n", count, a[0], a[count - 1],
      eol ? "Yes" : "No");
  free(a);
  return 0;
}
+1
source

Source: https://habr.com/ru/post/1584936/


All Articles