i want to use scan f týo get such a lýne as ýnput and store ýt ýn an array.what should ý do?
ex:100 12 23 43 53 64
i want to use scan f týo get such a lýne as ýnput and store ýt ýn an array.what should ý do?
ex:100 12 23 43 53 64
char array[10];
scanf("%s",&array);
Use fgets(), not scanf(). It's safer.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
char array[10];
scanf("%s",&array);
this does not work.afret i had done this i printed the all arrays(array[1], array[2],......)
but it printed 0.0000 0.0000
by the way i want to store floating points in the arrays.
If you really must use scanf(), try something like this:
Code:#include <stdio.h> #define MAX 10 int main(void) { float tmp, arr[MAX]; int i, j; i = 0; while (scanf("%f", &tmp) == 1) { arr[i] = tmp; i++; if (i == MAX) break; } for (j = 0; j < i; j++) printf("Element %i is %f\n", j, arr[j]); return (0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
[code]
#include<stdio.h>
int main()
{
int num[10];
for (int x=0; x<10;x++)
{
printf("Enter number :\t",x);
scanf("%d",&num[x]);
}
}
Inline declaration of variables is a C++ thing, so you can't do this:Originally posted by dayknight
Code:#include<stdio.h> int main() { int num[10]; for (int x=0; x<10;x++) { printf("Enter number :\t",x); scanf("%d",&num[x]); } }
>for (int x=0; x<10;x++)
What happens if the user enters a letter by mistake? This code does no validation.
>printf("Enter number :\t",x);
The x is not needed in this statement.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]