Like Tree1Likes
  • 1 Post By laserlight

Getting user input for structure variables

This is a discussion on Getting user input for structure variables within the C Programming forums, part of the General Programming Boards category; Hi all, I am a new programmer so sorry if this is an annoying question. I would very much appreciate ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Getting user input for structure variables

    Hi all,

    I am a new programmer so sorry if this is an annoying question. I would very much appreciate your help with the following. I am trying to get a user to input values for a created structure type called car_t. When I input random values the string input works for the newcar.make (it prints exactly what I entered), but whatever I put in for newcar.price it always prints that the value is 0.00000.

    Here's my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct {
        double price;
        char make[50];
        }car_t;
    
    int main()
    {
        car_t newcar;
    
    
        printf("Input make of car: ");
    
    
        fgets(newcar.make, 50, stdin);
    
    
        printf("\nInput price of car: ");
    
    
        scanf("%lf", &newcar.price);
    
    
        printf("The make is: %s", newcar.make);
    
    
        printf("The price is: $%lf", newcar.price);
    
    
        return 0;
    }
    Thanks for your help.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take the l out of line 29.

    Also... unless you are buying a billion dollar car there's no need for a double there... a float would probably be just fine.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Wow... I'm such a fool! So you only need %f for printf() but %lf for scanf()... Thanks so much I feel like an absolute goose.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I feel like an absolute goose.
    You should also turn off your webcam... LOL...

    No worries... glad you got it working.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    You should also turn off your webcam... LOL...
    ^ I don't get it ha sorry...

    I have an extension to this problem.

    I want to create a "fleet" of cars - an array of type car_t that can hold up to ten cars. So firstly i'm trying to create an 'addCar' function, that gets input from the user and then returns the structure. I could get it working using a return(*structure*) statement, but i'm trying to do it with passing by reference. Again, my string input gets printed correctly, but it crashes when I try to retrieve the value for the price.

    Here's my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    typedef struct {
        double price;
        char make[50];
        }car_t;
    
    
    void addCar(car_t *ptrCar);
    
    
    int main()
    {
        car_t newcar;
    
    
        addCar(&newcar);
    
    
        printf("\nMake of car is %s", newcar.make);
    
    
        printf("\nPrice of car is $%.2f", newcar.price);
    
    
        return 0;
    }
    
    
    void addCar(car_t *ptrCar)
    {
        printf("Input make of car: ");
    
    
        fgets(ptrCar->make, 50, stdin);
    
    
        printf("\nInput price of car: ");
    
    
        scanf("%lf", ptrCar->price);
    
    
        return;
    }
    Any ideas? Hopefully it's something stupid again that's easy to fix!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by themolejoel View Post
    ^ I don't get it ha sorry...
    You said you felt like a total goose... The humourous infrance was that you look like one too!

    I have an extension to this problem.

    I want to create a "fleet" of cars - an array of type car_t that can hold up to ten cars. So firstly i'm trying to create an 'addCar' function, that gets input from the user and then returns the structure. I could get it working using a return(*structure*) statement, but i'm trying to do it with passing by reference. Again, my string input gets printed correctly, but it crashes when I try to retrieve the value for the price.

    ...
    Any ideas? Hopefully it's something stupid again that's easy to fix!
    Not this time... Maybe one of the others can help...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by themolejoel
    Any ideas? Hopefully it's something stupid again that's easy to fix!
    Like CommonTater, I did not find a problem by inspection. However, I compiled your program with compiler warnings turned on, upon which the problem was obvious from the compiler's warning. This:
    Code:
    scanf("%lf", ptrCar->price);
    should have been:
    Code:
    scanf("%lf", &ptrCar->price);
    Moral of the story: compile with warnings turned on to a sufficiently high level.
    CommonTater likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    CommonTater and laserlight: Thank you so much for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure, user input and printing out results
    By alvarito in forum C Programming
    Replies: 3
    Last Post: 10-04-2011, 11:04 AM
  2. Setting a user's input as the structure name
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 05:23 AM
  3. user input type variables
    By pastitprogram in forum C++ Programming
    Replies: 1
    Last Post: 09-05-2008, 07:21 AM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Replies: 3
    Last Post: 11-05-2001, 04:00 PM

Tags for this Thread


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