Like Tree1Likes
  • 1 Post By ZuK

How to prompt user to scan numbers into an array

This is a discussion on How to prompt user to scan numbers into an array within the C Programming forums, part of the General Programming Boards category; I am fairly new to c language and i need to prompt the user of my program to input numbers ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    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. #2
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    The assignment
    Code:
    float vector1[VECTOR_LENGTH] = {Xv, Yv , Zv} ;
    Is to be made after the values of Xv, Yv and Zv have already been read.
    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. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    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. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by Jeffery1 View Post
    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. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64
    Quote Originally Posted by Jeffery1 View Post
    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. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Jeffery1 View Post
    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
    Last edited by ZuK; 05-17-2013 at 02:22 AM.
    AndiPersti likes this.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    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. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,497
    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.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Thanks heaps! everything works as i want it to now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2012, 09:35 AM
  2. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  3. C Function won't scan for user inputted value
    By kevin250 in forum C Programming
    Replies: 10
    Last Post: 10-21-2010, 10:41 PM
  4. C Function won't scan for user inputted values
    By kevin250 in forum C Programming
    Replies: 3
    Last Post: 10-20-2010, 08:47 PM
  5. Replies: 6
    Last Post: 04-12-2002, 08:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21