Thread: Don't know what to do

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    Don't know what to do

    Declare a structure of type "Car" to represent a motor car including the elements:
    (a) make (string)
    (b) model (string)
    (c) odometer (int)
    (d) manufactureDate (Date - see below)
    (e) purchaseDate (Date - see below)
    (f) petrolTank, capacity and current level (Tank - see below)

    You will need to also declare a structure of type "Date" to represent the:
    (a) day (int)
    (b) month (int)
    (c) year (int)

    You will need to also declare a structure of type "Tank" to represent the:
    (a) capacity (double)
    (b) currentLevel (double)

    (That is, you will have structures nested inside structures.)

    Write a program that reads (from the user or a file) the car information for a fleet of cars. Put this in an array called "Fleet" that can hold up to 10 cars. (The user and file input must be able to handle less than 10 cars as well.) The program is then to give the user the option of either displaying the fleet on the screen or saving it to a file.

    Write the following input functions: "getFleet", "getCar", "getDate", "getTank" (which gets the information from the user) and "readFleet" (which reads the information from a file).

    Write the following output functions: "showFleet", "showCar", "showDate", "showTank" (which prints the information to the screen) and "saveFleet" (which saves the information to a file).

    Your program is to have appropriate error checking for user input and file I/O.

    I don't really know what to do....this is what I've done so far..I hate C...

    #include <stdio.h>
    #include <string.h>
    #define STRSIZE 50
    #define SIZE_OF_FLEET 10

    typedef struct {
    int day;
    int month;
    int year;

    } Date;

    typedef struct {
    double capacity;
    double currentLevel;

    } Tank;

    typedef struct {
    char make[STRSIZE];
    char model[STRSIZE];
    int odometer;
    Date manufactureDate; /* structure within struct */
    Date purchaseDate; /* structure within struct */
    Tank petrolTank, capacity, currentLevel; /* structure within struct */

    } Car;

    void getCar(Car *pCar);
    void getFleet(Car paramfleet[]);
    void getTank(Tank *pTank);


    int main(void)
    {
    getCar(&Car);
    return 0;
    }

    void getFleet(Car paramfleet[])
    {

    }


    void getCar(Car *pCar)
    {
    printf("Enter the make of the car.\n");
    scanf("%s", &(*pCar).make[STRSIZE]);

    printf("Enter the model of the car.\n");
    scanf("%s", &(*pCar).model[STRSIZE]);

    printf("Enter the odometer number");
    scanf("%d", &(*pCar).odometer);
    }

    void getTank(Tank *pTank)
    {
    /* pTank->capacity; */
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First of all, you should edit your post to include code tags around your code.

    Let's look at this:
    Code:
    int main(void)
    {
    getCar(&Car);
    return 0;
    }
    You are calling the function getCar(), but look what you are passing. Remember that Car is a type of structure. Might it be better to do something like:

    Code:
    int main()
    {
        struct Car myCar;
        getCar(&myCar);
        return 0;
    }
    This way you are passing a stucture of type Car instead of passing it the structure name.

    Next we have:
    Code:
    void getTank(Tank *pTank)
    {
    /* pTank->capacity; */
    }
    That doesn't do anything at all. Maybe you meant:
    Code:
    double getTank(Tank *pTank)
    {
        return pTank->capacity;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, I've got a way to start for you. Go read the Announcements, and go edit your post, adding in the needed code tags.

    Other than that, it looks like a good start. So chug away at it. Break it down to small steps, and complete each task as you come to them. When you run into a particular specific problem, we'll be here to give you a hand, but we don't do your work for you. And, since you didn't really say where your problem is, I'd venture to say you're not going to get a lot of help until you specificly say what you're having a prolem with.

    One thing I did note is that your scanf calls seem to be all wrong. scanf takes pointers. Thus, if you have a pointer to an object, and you want to scan into it, don't dereference it, simply pass the pointer name:
    Code:
    char something[BUFSIZ];
    char *s = something;
    scanf( "%s", something ); /* or... */
    scanf("%s", s );
    The above is correct. But not:
    Code:
    char something[BUFSIZ];
    char *s = something;
    scanf("%s", *s );
    Additionally, had you tried compiling your code, you'd find that your call in main to getCar isn't actually passing an instance of Car to the function. You haven't declared a variable anyplace to pass around. Consider the following:
    Code:
    void foo( int *bar ); 
    
    int main( void )
    {
        int baz;
    
        foo( &baz );
        return 0;
    }
    ...more code here...
    Here, we actually declare an integer named baz, whose address we pass along to bar. You declare no variables.

    [edit]Curses, foiled again![/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    This is what I've done so far, but still not working like the question asks..

    #include<stdio.h>

    typedef struct {
    char make[10], model[10];
    int odometer;
    } car_t;
    typedef struct{
    int day, month, year, pyear, pday, pmonth;
    }date_t;
    typedef struct{
    double capacity, currentLevel;
    }tank_t;



    int getCar(car_t *c);
    void showCar(car_t c);
    int getDate( date_t *d);
    void showDate(date_t d);
    double getTank( tank_t *t);
    void showTank(tank_t t);



    int main(void)
    {
    car_t c;
    date_t d;
    tank_t t;
    if (getCar(&c) != 0)


    showCar(c);


    if (getDate(&d) != 0)


    showDate(d);



    if (getTank(&t) != 0)


    showTank(t);

    return 0;

    }

    int getCar(car_t *c)
    {
    printf("Enter the name of the car.\n");
    if (scanf("%s", c->make) != 1)
    return -1;

    printf("Enter the model of the car.\n");
    if (scanf("%s", c->model) != 1)
    return -1;

    printf("Enter the odometer reading of the car.\n");
    if (scanf("%d", &c->odometer) != 1)
    return -1;

    return 0;
    }

    void showCar(car_t c)
    {
    printf("\n");
    printf("Name of the car is %s\n", c.make);
    printf("Model of the car is %s\n", c.model);
    printf("Odometer reading of the car is %d\n", c.odometer);
    }


    int getDate(date_t *d)
    {
    printf("Enter the day of when the car was made.\n");
    if (scanf("%s", &d->day) != 1)
    return -1;
    printf("Enter the month of when the car was made .\n");
    if (scanf("%s", &d->month) != 1)
    return -1;
    printf("Enter the year of when the car was made.\n");
    if (scanf("%d", &d->year) != 1)
    return -1;

    printf("Enter the purchase day of when the car was made.\n");
    if (scanf("%s", &d->pday) != 1)
    return -1;

    printf("Enter the purchase month of when the car was made .\n");
    if (scanf("%s", &d->pmonth) != 1)
    return -1;

    printf("Enter the purchase year of when the car was made.\n");
    if (scanf("%d", &d->pyear) != 1)
    return -1;

    return 0;
    }

    void showDate(date_t d)
    {
    printf("\n");
    printf("The day the car was made %d\n", d.day);
    printf("Month the car was made %d\n", d.month);
    printf("The year the car was made %d\n", d.year);

    }

    double getTank(tank_t *t)
    {
    printf("Enter tank capcaity of the car.\n");
    if (scanf("%lf", &t->capacity) != 1)
    return -1;

    printf("Enter the current level of the tank.\n");
    if (scanf("%lf", &t->currentLevel) != 1)
    return -1;


    return 0;
    }

    void showTank(tank_t t)
    {
    printf("\n");
    printf("The capacity of the tank is %lf\n", t.capacity);
    printf("The current level is %s\n", t.currentLevel);

    }

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Honestly, some people are their own worst enemy!

    "Well, I've got a way to start for you. Go read the Announcements, and go edit your post, adding in the needed code tags."

    Make it easy for people to help you, read all of the replies you're given head their advice and explain what's not working. People are willing to help, but you're making it hard for them.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    I don't have the time to do that......I really need help though....I amlike exhausted

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    I don't have the time to do that......I really need help though....I amlike exhausted
    I wish I could help you - but I don't want to (to paraphrase my favourite Friends quote)
    DavT
    -----------------------------------------------

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Pay attention to format specifiers.
    Code:
    typedef struct
    {
       int day, month, year, pyear, pday, pmonth;
    }date_t;
    Code:
    int getDate(date_t *d)
    {
       printf("Enter the day of when the car was made.\n");
       if ( scanf("%s", &d->day) != 1 )
          return -1;
       printf("Enter the month of when the car was made .\n");
       if ( scanf("%s", &d->month) != 1 )
          return -1;
       printf("Enter the year of when the car was made.\n");
       if ( scanf("%d", &d->year) != 1 )
          return -1;
    
       printf("Enter the purchase day of when the car was made.\n");
       if ( scanf("%s", &d->pday) != 1 )
          return -1;
    
       printf("Enter the purchase month of when the car was made .\n");
       if ( scanf("%s", &d->pmonth) != 1 )
          return -1;
    
       printf("Enter the purchase year of when the car was made.\n");
       if ( scanf("%d", &d->pyear) != 1 )
          return -1;
    
       return 0;
    }
    Code:
    typedef struct
    {
       double capacity, currentLevel;
    }tank_t;
    Code:
    void showTank(tank_t t)
    {
       printf("\n");
       printf("The capacity of the tank is %lf\n", t.capacity);
       printf("The current level is %s\n", t.currentLevel);
    }
    And pay attention to your own design.
    Code:
    int main(void)
    {
       car_t c;
       date_t d;
       tank_t t;
       if ( getCar(&c) != 0 )
          showCar(c);
    
       if ( getDate(&d) != 0 )
          showDate(d);
    
       if ( getTank(&t) != 0 )
          showTank(t);
    
       return 0;
    }
    What value do the functions getCar, getDate, and getTank return on success? on failure?
    Last edited by Dave_Sinkula; 10-21-2004 at 12:49 PM. Reason: typo
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I don't have the time to do that......I really need help though....I amlike exhausted
    Adding code tags involves pressing the edit button, and typing in 13 characters. That takes less time than complaining that you "dont have the time to do that".

    If you're not going to make the effort to follow this board's rules, then I'm not going to make the effort to help you.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I don't have the time to do that......I really need help though....I amlike exhausted
    Enjoy your black marks (or red as they will soon be)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    Support initiative #1!

  12. #12
    ---
    Join Date
    May 2004
    Posts
    1,379
    F is for FAIL

  13. #13
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by waldo
    I don't have the time to do that......I really need help though....I amlike exhausted

    Are you serious??
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  14. #14
    The C-er
    Join Date
    Mar 2004
    Posts
    192

    Lightbulb

    I don't have the time to do that......I really need help though....I amlike exhausted
    Something else about this quote............... I can't quite figure out what ................ Plz help me guys........

    Sound familiar?

    I leave you to arrive at you own conclusions.

Popular pages Recent additions subscribe to a feed