Thread: Structures and arrays problem

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    45

    Structures and arrays problem

    Hey so i am using Dev C++ compiler on Windows 7 and was programming a piece of code that is supposed to do the following -

    Create a structure to store information about products for sale in a store. It should store information about the product name, the price, and the product number and then create an array of products called Inventory. Add five products to your inventory.

    But for some reason, which is unknown to me, I always seem to get a complier error.
    Any suggestions and input will be gladly taken

    and this is what i have so far -

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    struct product{
    char name[30];
    int product_number;
    double price;
    }
    
    
    void initProducts();
    
    
    int main() {
        product inventory[5];
        initProducts(inventory);
        printf("\n\n");
        system("pause");
    }
    
    
    void initProducts() {
         for(int i=0; i<5; i++) {
                 printf("\n\nEnter the name of the product: ");
                 scanf("%s", inventory[i].name);
                 fflush(stdin);
                 
                 printf("\n\nEnter the product number of the product: ");
                 scanf("%d", inventory[i].product_number);
                 fflush(stdin);
                 
                 printf("\n\nEnter the price of the product: ");
                 scanf("%f", inventory[i].price);
                 fflush(stdin);
        }
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    The struct declaration doesn't look like - you're missing the semi-colon at the closing brace.

    products inventory[5] is local to main; the initProducts function doesn't have access to it. Either pass it as a parameter (ideal) or make it global (yuk).

    Code:
    #define MAX_ENTRY 5
    
    typedef struct
    {  
         // fields
    } products;
    
    void initProducts(products *); // prototype
    
    main
        products inventory[MAX_ENTRY] = { 0 };
        initProducts(inventory);
        ...
    
    void initProducts(products *tmp)
    {
        for(i=0; i < MAX_ENTRY; ++i)
        {
             // store name
             scanf("%d", &tmp[i].product_number);
             scanf("%lf", &tmp[i].price);
        }
    }
    I don't think you'd want to use fflush(stdin) either; the last I heard, it was undefined for input streams.

    whenever you get a compiler error, you should make note of it in your topic.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In terms of your most important problems, I'd say:

    Critical:
    * You declare initProducts to take no parameters. Then you call it with parameters. Then you define it to take no parameters.
    * initProducts tries to access the parameter it doesn't have (but should).
    * struct definition missing a semicolon

    High:
    * Reading the name via scanf() has nothing in place that limits it to accepting 29 characters (as I assume you want to null terminate the string).
    * Your magic constant in initProducts should be at worst a constant defined elsewhere, or (better) accept the length of the array as a parameter you pass in.
    * No tolerance for bad input.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    45
    oh, that semi-colon
    and functions, gonna take some time for me to fully grasp that concept
    thanks guys !!!!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  3. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 12:31 PM
  4. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM
  5. problem with arrays and structures
    By gell10 in forum C++ Programming
    Replies: 7
    Last Post: 11-03-2003, 12:02 AM