Help with array of structure????

This is a discussion on Help with array of structure???? within the C Programming forums, part of the General Programming Boards category; im having a lot of trouble trying to find whats wrong with this program. Code: #include<stdio.h> #include<math.h> #include<conio.h> struct inventory ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Markham, Ontario, Canada
    Posts
    2

    Help with array of structure????

    im having a lot of trouble trying to find whats wrong with this program.
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    struct inventory {
      char itemname[20];
      int qty;
      float price;
    };
    
    int ask(inventory item[], int range);
    int display(float total);
    void main()
    {
      int t;
      struct inventory item1[100];
      printf("Hello and welcome to your inventory. \n");
      printf("\nHow many items would you like to store? \n");
      scanf("%i", &t);
      ask(item1, t);
    
    
      printf("Item        Quantity     Price     Total\n");
    
    
      getch();
    }
    
    int ask(inventory item[], int range)
    {
    
      int i = 0;
      printf("\nWhat is your item name? > ");
      fflush(stdin);
      gets(item[i].itemname);
    
    
      printf("\nHow many number of items you would like? > ");
      scanf("%d", &item[i].qty);
    
      printf("\nWhat is the price of your product? > ");
      scanf("%f", &item[i].price);
    
    
      return item[i].qty, item[i].itemname, item[i].price;
    
    
    }
    
    //int display(int total)
    //{
    //    int i=0;
    //    total=item[i].qty*item[i].price;
    //return total;
    //}
    //
    Last edited by Salem; 03-20-2013 at 12:46 AM. Reason: Use code tags!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    We'll start here:

    void main() should be int main() and the main function should return an integer value http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    You should never, ever use gets(). FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    You should never, ever use fflush(stdin). FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Beyond that, just throwing up some code and saying "What's wrong with this" is bad form. You need to tell us exactly what's happening that's different from what you expect. If you're getting errors, tell us exactly what those errors are. If the output is not what you expect, tell us what you use for input, what you expect for output, and what you actually get for output. If it's crashing, tell us what the operating system is telling you when it crashes.

    Get it?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Markham, Ontario, Canada
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    We'll start here:

    void main() should be int main() and the main function should return an integer value FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com
    You should never, ever use gets(). FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    You should never, ever use fflush(stdin). FAQ > Why fflush(stdin) is wrong - Cprogramming.com

    Beyond that, just throwing up some code and saying "What's wrong with this" is bad form. You need to tell us exactly what's happening that's different from what you expect. If you're getting errors, tell us exactly what those errors are. If the output is not what you expect, tell us what you use for input, what you expect for output, and what you actually get for output. If it's crashing, tell us what the operating system is telling you when it crashes.

    Get it?
    error C2146: syntax error : missing ')' before identifier 'item'
    error C2061: syntax error : identifier 'item'
    error C2059: syntax error : ';'
    error C3409: empty attribute block is not allowed
    error C2059: syntax error : ','
    error C2059: syntax error : ')'
    'ask' undefined; assuming extern returning int
    syntax error : missing ')' before identifier 'item'
    syntax error : identifier 'item'
    syntax error : ';'
    empty attribute block is not allowed
    syntax error : ','
    syntax error : ')'



    fflush and gets is needed to get a sentence from the user

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Compare this:

    Code:
    struct inventory item1[100];  // this is good
    ... with this:

    Code:
    int ask(inventory item[],int range);  // something's missing here
    --------

    fflush and gets is needed to get a sentence from the user
    "fflush(stdin)" is not needed to get input from the user. If there's data left on the input buffer, then there are more proper ways of clearing it in the third link provided by rags_to_riches. "fflush(stdout)" might be needed to ensure the previous line is printed out to the screen - or you can just add a newline at the end of the printf string.

    The second link provided by rags_to_riches shows a safe alternative to "gets()" called "fgets()". You should take the advice given and avoid "gets()".

  5. #5
    Registered User hex_dump's Avatar
    Join Date
    Dec 2012
    Posts
    88
    Quote Originally Posted by Charlitoo View Post
    error C2146: syntax error : missing ')' before identifier 'item'
    error C2061: syntax error : identifier 'item'
    error C2059: syntax error : ';'
    error C3409: empty attribute block is not allowed
    error C2059: syntax error : ','
    error C2059: syntax error : ')'
    'ask' undefined; assuming extern returning int
    syntax error : missing ')' before identifier 'item'
    syntax error : identifier 'item'
    syntax error : ';'
    empty attribute block is not allowed
    syntax error : ','
    syntax error : ')'



    fflush and gets is needed to get a sentence from the user
    look up using fgets() instead as gets() is not only bad practice but also has been deprecated in std C.
    For your problem, look at the errors returned from your compiler. Syntax error means you wrote something wrong. Possibly spelt something wroong....?

  6. #6
    nmn
    nmn is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    this code:
    Code:
    int ask(inventory item[], int range);
    should be:
    Code:
    int ask(struct inventory *item, int range);
    and correspondingly make changes in the function definition

  7. #7
    nmn
    nmn is offline
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    also how can you expect the function to return 3 different values at the same time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Want help in Structure Array !!
    By smoker91 in forum C Programming
    Replies: 1
    Last Post: 12-24-2011, 03:56 AM
  2. Little bit help for Array Structure
    By cc8163 in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2011, 05:49 PM
  3. Dynamic structure with array and array count
    By Nazgulled in forum C Programming
    Replies: 14
    Last Post: 06-08-2007, 11:10 PM
  4. structure array
    By disco_stu in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 08:55 AM
  5. structure with 2-dim array
    By sballew in forum C Programming
    Replies: 8
    Last Post: 10-27-2001, 05:30 PM

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