# Thread: How to prompt user to scan numbers into an array

1. ## How to prompt user to scan numbers into an array

I am fairly new to c language and i need to prompt the user of my program to input numbers into an array so that later on these numbers can be added or subtracted with other numbers to form a new array. My problem is I don't know how to make the user input numbers which will then be saved into the array for later use. Here is the parts of my code that relate to the problem:
Code:
float Xv, Yv, Zv, Xu, Yu, Zu ;
float vector1[VECTOR_LENGTH] = {Xv, Yv, Zv} ;

scanf("%1f %1f %1f", &Xv, &Yv, &Zv);

printf("The first element in the array vector1 is: %3f \n\n", vector1[0]);
The point of that printf function is to see if what they have entered is actually registering as what i want it to. This does not work however and the value for this always comes up as 0.

Can someone please show me how to scan numbers into an array so that they can be used for later use?

Thanks for any help in advance.

2. The assignment
Code:
float vector1[VECTOR_LENGTH] = {Xv, Yv , Zv} ;
The code may be

Code:
float Xv, Yv, Zv, Xu, Yu, Zu;
float vector1[VECTOR_LENGTH];
scanf("%1f %1f %1f, &Xv, &Yv, &Zv);
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv}; //Notice the new Position of the asignment.
printf("The first Element in the array Vector1 is : %3f \n\n", vector1[0]);
Did This help?????

3. try
Code:
scanf("%1f %1f %1f", &vector1[0], &&vector1[1], &&vector1[2]);
for a more general solution you can use a loop

Code:
int idx;
for ( idx = 0; idx < VECTOR_LENGTH; ++idx )
scanf("%1f", &vector1[idx]);
your format looks strange, it inputs only 1 digit integers
I'd just use "%f"

Kurt

4. This is great! i understand the difference. The only problem that i now have is when i try to compile it i get the error:
Code:
"error: expected expression before ‘{’ token"
this error is on the line
Code:
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv};
Besides that I cant see why it wouldnt work

5. Originally Posted by Jeffery1
This is great! i understand the difference. The only problem that i now have is when i try to compile it i get the error:
Code:
"error: expected expression before ‘{’ token"
this error is on the line
Code:
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv};
Besides that I cant see why it wouldnt work
There seems to be no error in this line Try sending the whole code. At times the error is before the indicated line

6. Originally Posted by Jeffery1
This is great! i understand the difference. The only problem that i now have is when i try to compile it i get the error:
Code:
"error: expected expression before ‘{’ token"
this error is on the line
Code:
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv};
Besides that I cant see why it wouldnt work
There seems to be no error in this line Try sending the whole code. At times the error is before the indicated line

7. Originally Posted by Jeffery1
Code:
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv};
Besides that I cant see why it wouldnt work
This is not valid you can initialize only when you declare a variable
This would be assignement and arrays cannot be assigned to.
Kurt

8. Here is the whole code with the error.

Code:
#include <stdio.h>

#define VECTOR_LENGTH 3

int main (void)
{
/* declare variables */
int option, i;
float Xr, Yr, Zr ;
float Xv, Yv, Zv, Xu, Yu, Zu ;
float vector1[VECTOR_LENGTH] ;

scanf("%1f %1f %1f", &Xv, &Yv, &Zv);
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv} ;
printf("The first Element in the array Vector1 is : %3f \n\n", vector1[0]);
the error is apparently on the line
Code:
vector1[VECTOR_LENGTH] = {Xv, Yv, Zv} ;
and the error is
Code:
error: expected expression before ‘{’ token
can anyone tell me why this error is coming up and how i could go about fixing it so that the assigned values come up when i use the printf function.

9. can anyone tell me why this error is coming up and how i could go about fixing it so that the assigned values come up when i use the printf function.
The answer is in Zuk's response -> You can't assign 3 values like that, you need to assign the values one at a time.

10. Thanks heaps! everything works as i want it to now.