Thread: Problem with the purchase function

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Problem with the purchase function

    Hello. I wrote this program and it has problems in the purchase function. It's meant to give the user an option of what to buy and ask the user how much of a certain product does he/she wants to buy and then do the calculations and display how much money the user has left. I tried to solve it but without any luck plus i'am a bit stuck on this problem and I cannot write the next part of my program untill I overcome this problem. The compiler keeps saying uknown field and I don'nt really understand what it means by it. I would appriciate any help.

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 9
    
    typedef struct{
      char Goods[20];
    }Goods_g;
    
    typedef struct{
      char PortName[10];
    }Ports_p;
    
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[]);
    
    int Purchase(Goods_g *G, float variables[], float money); /*fixed the problem with the prototype first*/
    
    int main()
    {  
      Goods_g G[9]= {
    		{ "Fish" },
                 { "Cotton" },
                 { " Wood" },
                 { "Coco" },
                 { "Sugar" },
                 { "Guns" },
                 { "Textiles" },
                 { "Tobacco" },
                 { "Wine" },
    		};
    
      Ports_p P[6] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
      char name[A];
      int i;
      int j;
      int k;
      float variables[SIZE];
     float money = 1000;
      printf("Please enter your name: ");
      scanf("%s",name);
    
      printf("Please select which port you want to start in: ");
      scanf("%d",&i);
    
      srand((unsigned)time(NULL));
      for (j=0; j<SIZE; j++)
        variables[j] = (rand()%26)/14.0f;
    
      if ( i >= 1 && i <= 6 ) {
        DisplayPort ( &G[k-1], &P[i-1], variables);
      }
      
    	if ( k >=1 && k<= 9) {
    Purchase(&G[k-1], variables, money); /*that's the function call fixed*/
    }
    
      return 0;
    }
    
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[])
    {
      G->Fish   = 50*variables[0];
      G->Cotton = 80*variables[1];
        G->Wood = 100*variables[2];
      G->Coco = 140*variables[3];
      G->Sugar = 350*variables[4];
      G->Guns = 700*variables[5];
      G->Textiles =1350*variables[6];
      G->Tobacco =1900*variables[7];
      G->Wine =2150*variables[8];
    
      printf("Welcome to %s\n",P->PortName);
      printf("The prices for the material is as follows\n");
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
       printf("Wood=%f\n", G->Wood);
      printf("Coco=%f\n", G->Coco);
      printf("Sugar=%f\n", G->Sugar);
      printf("Textiles=%f\n", G->Textiles);
      printf("Tobacco=%f\n", G->Tobacco);
      printf("Wine=%f\n", G->Wine);
    
      return 0;
    }
    
    int Purchase(Goods_g *G, float variables[], float money) /*fixed definition*/
    {
      G->Fish   = 50*variables[0];
      G->Cotton = 80*variables[1];
      G->Wood = 100*variables[2];
      G->Coco = 140*variables[3];
      G->Sugar = 350*variables[4];
      G->Guns = 700*variables[5];
      G->Textiles =1350*variables[6];
      G->Tobacco =1900*variables[7];
      G->Wine =2150*variables[8];
      
    	int amount;
    	float totals;
    	float totalm;
    
    printf("How many individual units of %s do you want to buy: ", G->Goods);
    scanf("%d", &amount);
    
    
    
                 totals = (int) G->Goods*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of %s\n",amount, G->Goods);
    
    return 0;
    	}

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Ok this is your ninth thread about this program. On one occasion
    i solved a problem of yours and the code worked, and it appears
    that each following thread you are making modifications that you
    dont understand and introducing new errors into the code. My
    advice is to learn the language. Try getting a reputable book or
    a tutorial and really study because some of the errors in your
    code are so fundamental that it is really worrying.

    What compiler are you using? If you dont understand your
    compiler error messages, either look up your documentation,
    or get a better compiler. Both compilers i use show up the
    following errors:

    in function int DisplayPort(Goods_g*,Ports_p*,Float*):
    struct Goods_g has no member named "Fish"
    struct Goods_g has no member named "Cotton"
    struct Goods_g has no member named "Wood"
    .
    .
    .
    .
    in function int Purchase(Goods_g*,float*,float):
    struct Goods_g has no member named "Fish"
    struct Goods_g has no member named "Cotton"
    struct Goods_g has no member named "Wood"
    See the problem?

    Here is your definition of Goods_g:

    Code:
    typedef struct{
      char Goods[20];
    }Goods_g;
    now stick with me because this is the start of your trouble.
    Goods is a character array (string). it is the only
    member within the struct.

    in main you declare an array of structs, type Goods_g.
    you initialise the members as follows:

    Code:
      Goods_g G[9]= {
    		{ "Fish" },
                 { "Cotton" },
                 { " Wood" },
                 { "Coco" },
                 { "Sugar" },
                 { "Guns" },
                 { "Textiles" },
                 { "Tobacco" },
                 { "Wine" },
    		};
    ask yourself, what is happening here? you are declaring 10
    structs of type Goods_g, and you are intitialising the members
    of that array, 1 member per array, to names you have chosen.
    essentially what the above code does is set the Goods string
    in the 0 element of the Goods_g array to the string literal "Fish"
    the 1 element gets its member set to "Cotton".
    See the pattern?

    Now thats all good and well, that works. Then later, you call
    DisplayPort, and you pass the memory address of one of those
    elements of the array of structs to the function.

    DisplayPort ( &G[k-1], &P[i-1], variables);

    the problem with that is that "k" is a variable and it is uninitialised
    at this point in the program so even if the next point which i will
    make was correctly coded by you, that function call would mess
    it up. Lets assume that you were passing element 0 of the
    array of structs. Here is the motherload of a problem
    which you have created:

    Code:
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[])
    {
      G->Fish   = 50*variables[0];
      G->Cotton = 80*variables[1];
        G->Wood = 100*variables[2];
      G->Coco = 140*variables[3];
      G->Sugar = 350*variables[4];
      G->Guns = 700*variables[5];
      G->Textiles =1350*variables[6];
      G->Tobacco =1900*variables[7];
      G->Wine =2150*variables[8];
    
      printf("Welcome to %s\n",P->PortName);
      printf("The prices for the material is as follows\n");
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
       printf("Wood=%f\n", G->Wood);
      printf("Coco=%f\n", G->Coco);
      printf("Sugar=%f\n", G->Sugar);
      printf("Textiles=%f\n", G->Textiles);
      printf("Tobacco=%f\n", G->Tobacco);
      printf("Wine=%f\n", G->Wine);
    
      return 0;
    }
    when you pass the address of that element, you can manipulate
    its members using the pointer you have set up (i.e. the string
    Goods). The problem you have created in the above code is
    that you are trying to the access member Fish, the member
    Cotton, and so on.

    NONE OF THESE ARE ACTUALLY MEMBERS OF THAT BLOODY
    STRUCT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    the address you passed, points to a string, you initialised that
    string to "Fish". to break it down as simply as possible,
    you passed a pointer to an array of characters containing the
    word Fish. Fish is not a member of the struct, neither is Cotton.
    the only member is the array that contains the
    string literal Fish!


    Do you see the problem??? None of that code make a wink of
    sense in any language. Now i could go into even more detail
    for you but i neither have the time nor the patience to explain
    any further. Just learn how to program and learn from this,
    at least in a small way that your code is remarkably ,
    fundamentally, and morally wrong. It will never be right.
    the reason that no-one responded to your last thread in a helpful
    manner is because you code is so wrong that it would take so
    long to explain to you what is wrong. I wasn't even gonna reply
    myself but i thought it only fair for you to get some form of
    feedback on your code. i dont mean to sound offensive, but
    what i have said is the truth as i see it
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM