Multiple structs and outputting them

This is a discussion on Multiple structs and outputting them within the C Programming forums, part of the General Programming Boards category; The code is essentially complete but I am having difficulty outputting the numerical inputs. The areas that are causing the ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    21

    Multiple structs and outputting them

    The code is essentially complete but I am having difficulty outputting the numerical inputs. The areas that are causing the program to fail have been commented out.

    Code:
      #include <stdio.h>
      #include <string.h>
    
      typedef struct
           { char make[15];
             char model[15];
             int odom;
           }auto_t;
    
      typedef struct
           { char manmonth[10];
             int manday;
             int manyear;
             char purmonth[10];
             int purday;
             int puryear;
           }date_t;
    
      typedef struct
           { int tankcap;
             double tankcur;
           }tank_t;
    
    
      void scan_auto (auto_t *a_car);
      void scan_dates (date_t *a_date);
      void scan_tank (tank_t *a_tank);
      void print_auto (auto_t a_car);
      void print_dates (date_t a_date);
      void print_tank (tank_t a_tank);
     
    
     int main(void)
      {
       auto_t this_car;
       scan_auto(&this_car);
       date_t this_date;
       scan_dates(&this_date);
    /* tank_t this_tank;
       scan_tank(&this_tank);*/
       print_auto(this_car);
       print_dates(this_date);
    /* print_tank(this_tank);*/
       return 0;
      }
    
      void scan_auto(auto_t *a_car)
      {printf("Enter make of automobile: ");
       gets((*a_car).make);
       printf("Enter model of automobile: ");
       gets((*a_car).model);
    /* printf("Enter mileage of automobile: ");
       gets((*a_car).odom);*/
       return;
      }
      void scan_dates(date_t *a_date)
      {printf("Enter the month of manufacturing: ");
       gets((*a_date).manmonth);
    /* printf("Enter the day of manufacturing: ");
       gets((*a_date).manday);
       printf("Enter the year of manufacturing: ");
       gets((*a_date).manyear);*/
       printf("Enter the month of purchase: ");
       gets((*a_date).purmonth);
    /* printf("Enter the day of purchase: ");
       gets((*a_date).purday);
       printf("Enter the year of purchase: ");
       gets((*a_date).puryear);*/
       return;
      }
      void print_auto(auto_t a_car)
      {printf("Entering this automobile\n");
       printf("Make is %s\n", a_car.make);
       printf("Model is %s\n", a_car.model);
    /* printf("Mileage is %s\n", a_car.odom);*/
       return;
      }
    
      void print_dates(date_t a_date)
      {printf("Manufactured in %s\n", a_date.manmonth);
    /* printf("on the %sth day of", a_date.manday);
       printf("%s", a_date.manyear);*/
       printf("Purchased in %s\n", a_date.purmonth);
    /* printf("on the %sth day of", a_date.purday);
       printf("%s", a_date.puryear);*/
       return;
      }
    
    /*void scan_tank(tank_t *a_tank)
      {printf("Enter the tank capacity: ");
       gets((*a_tank).tankcap);
       printf("Enter current fuel level in gallons: ");
       gets((*a_tank).tankcur);
       return;
      }
    
      void print_tank(tank_t a_tank)
      {printf("Tank capacity: %s gallons\n", a_tank.tankcap);
       printf("Current tank level: %s gallons\n", a_tank.tankcur);
       return;
      }*/
    I imagine it's something simple such as scanning the data in as the wrong datatype but I couldn't figure it out.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    1. Never ever use gets, use fgets instead, read the FAQ
    2. You cannot use fgets (or gets) on anything but strings. A sensible way to read ints would be fgets into a buffer, followed by sscanf, or strtol to convert to an int. Read the FAQ.

    References:
    gets sucks FAQ
    validate input FAQ

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, how do u scan a number from the user
    Code:
     printf("Enter mileage of automobile: ");
       gets((*a_car).odom); // scanf("%d",&(*a_car).odom);
    you have made the same mistake again in the other three function. to read integer u dont need to use gets fucntion unless the u need to read a string from the user. gets fucntion is only to be used if u are reading string from the users. to be honest gets fucntion shouldn't be used!! why ?? read the FAQ u gets a very good reason of why gets fucntion should be used. instead u can use fgets which is meant to read a string from a specified input stream here is am sample code of how to use fgets

    Code:
    char str[10];
    
    fgets(str,sizeof(str),stdin);   //note: u are specifing the imput stream as stdin standard input thats the keyboard
    make sure u read the FAQ and make changes in your code so that u dont read integer using get fucntion use scanf instead

    ssharish2005

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Yes, that's more or less your problem. gets() takes a pointer to a char... ie, a string. However, I recommend not using gets(), since it doesn't check for buffer overruns. (Where you type "foo", someone will type, "foofoofoofoo...") There's probably a few FAQ articles on this.

    Also:
    Code:
    (*a_tank).tankcur
    is equivalent to:
    Code:
    a_tank->tankcur
    Where the latter is much neater. Look up the -> operator.

    As for reading numbers, you'll need to convert the input from a string. You can use scanf() or a variant thereof, or do it yourself, perhaps using strtod (for doubles) and strtol.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    ssharish2005,
    Code:
    scanf("%d",&(*a_car).odom);
    That won't cope well at all if something other than an integer is entered. See the FAQ I posted above for better solutions.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by cwr
    ssharish2005,
    Code:
    scanf("%d",&(*a_car).odom);
    That won't cope well at all if something other than an integer is entered. See the FAQ I posted above for better solutions.
    hope this workd fine for that
    Code:
     scanf("%d",&a_car->odom);
    thax for pointing me out CWR

    ssharish2005

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    scanf("%d",&a_car->odom);
    That won't cope well, either. It's the same thing. See the FAQ!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    so is that should be like this

    Code:
    fgets(a_cars->odom,10,stdin);
    provided odom is declared a char * in the structure

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help to sort array of structs
    By sass208 in forum C Programming
    Replies: 1
    Last Post: 11-25-2006, 11:11 PM
  2. Inputting and outputting from structs
    By ashcan1979 in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2006, 04:31 AM
  3. Help with pointers and structs
    By NewToC in forum C Programming
    Replies: 8
    Last Post: 12-13-2002, 06:04 AM

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