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");
}