Struct Trouble

This is a discussion on Struct Trouble within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am currently working on an assignment in my C language class that has me creating a struct ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    1

    Struct Trouble

    Hello all,
    I am currently working on an assignment in my C language class that has me creating a struct file, writing it to a binary file, then reading the file to import the data into a table. I am currently stuck as my code is only bring in the headers and no actual data for what I need. If anybody can help I would greatly appreciate it! My code looks like this:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    #define Company ("Sierra Sporting Goods")
    typedef struct
    {
        double P[6];
    int NUMBER, PRODUCT, QUANTITY;
    double COST, PRICE;
    char DESCRIPTION[18];
    }INVENTORY;
    void add();
    void show(int NUMBER, int QUANTITY, double COST, double PRICE);
    double total(double A, double B);
    int getint(int MIN,int MAX, char prompt[]);
    double getreal(double dMIN, double dMAX, char prompt[]);
    int menu();
    void show(INVENTORY rec, double COST, double PRICE, double profit, char DE[]);
    void init_costs(double *P, int c);
    void show_costs(double *P, char **DEPARTMENT, int c);
    void gets(char entry[], char prompt[]);
    void strcase(char *s);
    void report();
    
    
    char *DEPARTMENT[] = {"Camping", "Tennis", "Golf", "Snow Sports", "Water Sports"};
    
    
    
    
    int main(void)
    {
        int key;
    do
        {
        key = menu();
        switch (key)
            { 
            case 1: add(); break;
            case 2: report(); break;
            case 3: 
            case 4: 
            case 5: return 0; break;
            default: printf("\nError choose again.");
            }
        }
        while (key);
    return 0;
        }
    
    
    int menu(void)/*Main Menu*/
    {
        int choice;
        printf("\n%s\n", Company);
    
    
        printf("1 = Add a record\n");
        printf("2 = Report\n");    
        printf("3 = Delete a record\n");
        printf("4 = Change a record\n");
        printf("5 = Quit\n");
        choice = getint(1, 5, "Enter your selection: \n ");
        return(choice);
    }
    
    
    
    
    void add()/*Calls functions(main part of program)*/
    {
    
    
    double total_price, total_cost, profit;
    char choice;
    int readValues, items, c; 
    int rNUMMIN = 0, rNUMMAX = 0, rTYPEMIN = 0, rTYPEMAX = 0, rQUANTITYMIN=0, rQUANTITYMAX=0;
    double rCOSTMIN=0, rCOSTMAX=0, rPRICEMIN = 0, rPRICEMAX = 0;
    INVENTORY rec;
    
    
        FILE *fp;    
        if ((fp = fopen("inventory.dat", "ab")) == NULL)
        {
            printf("Error opening file.");
            exit(1);
        }
        
        FILE *fr;
        if((fr = fopen("limits.txt", "r")) == NULL)
        {
        printf("File does not exist");
        exit (1);
        }
        else{
            readValues = fscanf(fr, "%d%d%d%d%d%d%lf%lf%lf%lf", &rNUMMIN, &rNUMMAX, &rTYPEMIN, &rTYPEMAX, &rQUANTITYMIN, &rQUANTITYMAX, &rCOSTMIN, &rCOSTMAX, &rPRICEMIN, &rPRICEMAX);
            
            if(readValues != 10){
                printf("Unable to get limits");
                exit(1);
            }
        }
        
    init_costs(rec.P, 6);
    fseek(fp, 0L, SEEK_END);
    c = (int)ftell(fp) / sizeof(rec);
    do
    {    
        rec.NUMBER = getint(rNUMMIN, rNUMMAX, "product number: \n\n");
        rec.PRODUCT = getint(rTYPEMIN, rTYPEMAX, "product type: \n\n");
        gets(rec.DESCRIPTION, "description: \n\n");
        strcase(rec.DESCRIPTION);
        rec.QUANTITY = getint(rQUANTITYMIN, rQUANTITYMAX, "Quantity: \n\n");
        rec.COST = getreal(rCOSTMIN, rCOSTMAX, "product cost: \n\n");
        rec.PRICE = getreal(rPRICEMIN, rPRICEMAX, "product price: \n\n");
        
        total_cost = total(rec.COST, rec.QUANTITY);
        total_price = total(rec.PRICE, rec.QUANTITY);
        profit = total_price - total_cost;
    
    
        rec.P[rec.PRODUCT] += total_price;
        
        show_costs(rec.P, DEPARTMENT, 6);
    
    
        show(rec, total_cost, total_price, profit, rec.DESCRIPTION);
        
        printf("Enter another product: Y/N?");
        scanf("%c%*c", &choice); 
    if (choice == 'y' || choice =='Y')
    {
    items = fwrite(&rec, sizeof(rec), 1, fp);
    if (items == (int)NULL) printf("Error writing to file");
    else c++;
    }
    }
    while( choice == 'y' || choice == 'Y');
    fclose(fr);
    fclose(fp);
        return;
    }
    
    
    
    
    int getint(int MIN, int MAX, char prompt[])/*Min Max statement for values of number product and quantity*/
    {
    int a;
    
    
    printf("\nEnter the %s %d to %d: ", prompt, MIN, MAX);
    scanf("%d%*c", &a);
    while ( a < MIN || a > MAX)
        {
        printf("\nInvalid, try again.\n");
        printf("\nEnter the %s %d to %d: ", prompt, MIN, MAX);
        scanf("%d%*c", &a);
        }
    return (a);
    }
    
    
    
    
    double getreal(double dMIN, double dMAX, char prompt[])/*Function for cost and price*/
    {    double a;
    
    
            printf("Enter the %s %.2f to %.2f: ", prompt, dMIN, dMAX);
            scanf("%lf%*c", &a);
            while ( a < dMIN || a > dMAX)
        {
        printf("\nInvalid, try again.\n");
        printf("Enter the %s %.2f to %.2f: ", prompt, dMIN, dMAX);
        scanf("%lf%*c", &a);
            }
        return(a);
    }
    
    
    double total(double A, double B)/*Function to be called for total price cost and profit*/
    {    
        double n;
        double total_cost, total_price, profit;
        total_cost = (A * B);
        total_price = (A * B);
        return (total_cost);
        return (total_price);
        return(n);
    }
    
    
    void init_costs(double *P, int c)/*Intializes chart to 0*/
    {
        int i;
    
    
        for (i = 1; i < c; i++)
            *(P + i) = 0;
    }
    
    
    void gets(char entry[], char prompt[])
    {
    printf("Enter the %s: ", prompt);
    gets(entry);
    }
    
    
    void strcase(char *s)/*Uppercase the description*/
    {
          int i = 0;
     
          *s = toupper(*s);
          s++;
        while (*s != NULL)
        {
          if (*s == ' ')
        {
         *(s+1) = toupper(*(s+1));
         s++;
         }
        else
        {
        *s = tolower(*s);
         }
          s++;
          }
    }
    
    
    void show_costs(double *P, char **DEPARTMENT, int c)/*Show costs by department*/
    {
        int i;
        double total = 0;
    
    
    printf("Total by category        Department\n");
    
    
    
    
    for (P++, i = 1; i < c; i++, P++, DEPARTMENT++)
    {
        printf("%2d%30s%20.2lf\n", i, *DEPARTMENT, *P);
        total += *P;
    }
    
    
    printf("%52.2lf\n", total);
    }
    
    
    void show(INVENTORY rec,  double total_cost, double total_price, double profit, char DESCRIPTION[])/*Show grand totals*/
    {
    printf("\nThe product number is--> %d.", rec.NUMBER);
    printf("\nThe product type is----> %d.", rec.PRODUCT);
    printf("\nThe description is-----> %4s.", rec.DESCRIPTION);
    printf("\nThe quantity is--------> %d.", rec.QUANTITY);
    printf("\nThe cost is------------> %.2lf.", total_cost);
    printf("\nThe price is-----------> %.2lf.", total_price);
    printf("\nThe profit is----------> %.2lf.", profit);
    
    
        printf("\nPress Enter to continue");
        getchar();
    }
    
    
    void report()
    {    
        INVENTORY rec;
        int c, num = 0; 
        double profit;
        
        FILE *fp;
        printf ("\n\n", Company);
        fp = fopen("inventory.dat", "rb");
        if (fp == NULL) { printf("Error opening file.\a");
        return; 
        }
    printf("Prod#  Type Description         Qty Cost Price Profit\n\n");
    while (fread(&rec, sizeof(rec), 1, fp))
    {
        ++num;
        printf("%04d %d %s %d %.2lf %.2lf %.2lf\n\n", rec.NUMBER, rec.PRODUCT,  rec.DESCRIPTION, rec.QUANTITY, rec.COST, rec.PRICE, profit);
    }
    printf("\nDone\n");
    fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Phew, that's a lot of code! Also should be in "C Programming" rather than "C++ Programming".

    It looks pretty much ok to me. This bit looks wrong:
    Code:
    if (choice == 'y' || choice =='Y')
    {
    items = fwrite(&rec, sizeof(rec), 1, fp);
    if (items == (int)NULL) printf("Error writing to file");
    else c++;
    }
    The record will only be stored if the user says 'y' to entering another one, so you'll always lose the last record input.

    From your post it sounds like you're not managing to read anything at all? Is the file actually being created?

    If you're getting garbage printed then you've probably changed your struct since you last wrote the file, or maybe the file was originally output on a different OS or with a different compiler. Probably easiest to delete it and start over in that case.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Whilst it looks like C, it needs a C++ compiler.
    Code:
    void gets(char entry[], char prompt[])
    {
    printf("Enter the %s: ", prompt);
    gets(entry);
    }
    C does not do function overloading.

    Nevermind the train wreck that is gets(), for which the OP should read the FAQ.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Can you please explain the problem in a bit more detail (say including an example), and also try and cut out bits of the code which are not related to the problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble using gets(var) for struct data
    By Crypto. in forum C Programming
    Replies: 4
    Last Post: 03-09-2011, 12:37 PM
  2. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 01:49 PM
  3. Having trouble accessing members of a struct
    By Swarvy in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2009, 09:47 AM
  4. struct array trouble
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-27-2006, 02:20 PM
  5. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 11:54 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