Can'nt figure out ERROR

This is a discussion on Can'nt figure out ERROR within the C Programming forums, part of the General Programming Boards category; Hello There is a problem in the function purchase but I just can'nt put my finger on it can someone ...

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

    Can'nt figure out ERROR

    Hello There is a problem in the function purchase but I just can'nt put my finger on it can someone please help me or give me hints.

    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Great, so how about telling us what problem you're having, rather than just spitting up a bunch of code?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, if it has "errors", post any compiler output. If it compiles, detail what it should do, what it actually does, and what it's doing wrong.

    The function Purchase:
    In your structure Goods_g has no members named Fish, Cotton, Woods, etc. You'll need to add them to the structure if you want to set them.
    Not an error, but you should move variable declarations to the top of the function.
    Edit: "variables" seems a poor choice to name a variable.
    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)

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

    problem in the Goods_g structure

    Code:
    C:\Documents and Settings\hfa\Desktop\C\5.c(72): error #2152: Unknown field 'Fish' of 'Goods_g'.
    I think there is a problem in the way I modified the goods structure. The compiler keeps telling me the above line and it does it for everytime the a variable from the goods structure is used in the code. I'am sorry about before.

    And one other thing is that what do you actually mean by

    "The function Purchase:
    In your structure Goods_g has no members named Fish, Cotton, Woods, etc. You'll need to add them to the structure if you want to set them."

    The structure Goods_g is being sent to the function. I would appriciate more explaination on on what you meant.

  5. #5
    old man
    Join Date
    Dec 2005
    Posts
    90
    You're trying to use the contents of a struct member as if it's a member name, which it definitely is not ... you can refer to 'G[0]->Goods' but not 'G->Fish' ...
    Last edited by eerok; 01-26-2006 at 11:59 PM.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    It appears the offending lines are right at the top of the code.

    Code:
    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()
    {

    You declare types Goods_g and Ports_p, but you don't define them in main(). Try something like this.


    Code:
    /*This is where the variables get declared, but NOT defined*/
    struct goods{
      char Goods[20];
    };
    
    struct ports{
      char PortName[10];
    };
    
    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(void)
    {  
    
    /*Here is where I define the variables*/
    struct goods Goods_g;
    struct ports Ports_p;
    I didn't use typedefs because it brings back some pretty horrible flashbacks of FORTRAN programming on the VAX.

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by cdalten
    I didn't use typedefs because it brings back some pretty horrible flashbacks of FORTRAN programming on the VAX.
    Another VAX jockey eh? I rode that machine pretty hard way back when. Macro and C, mostly, with a smattering of PL/1 thrown in. Never did FORTRAN though.

    Regarding the original poster's problem: Not being flip or anything, but it is usually a good idea to try and eliminate ALL compiler errors before you start trying to figure out why something isn't running....

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by fgw_three
    Another VAX jockey eh? I rode that machine pretty hard way back when. Macro and C, mostly, with a smattering of PL/1 thrown in. Never did FORTRAN though.

    Regarding the original poster's problem: Not being flip or anything, but it is usually a good idea to try and eliminate ALL compiler errors before you start trying to figure out why something isn't running....
    I should have ran the code through the compiler. I just pointed out the most obvious error in the code. I figured start with what I know (or think I know) and move from there.

    Off topic: Yeah, I took a few weeks of FORTRAN programming using OpenVMS on the VAX a long time ago. I dropped the class halfway through the term because I could never get my butt out of bed by 7:45am to make it to the 8am class.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > char Goods[20];
    Probably because the rest of the code assumes the old way of doing things like you did here
    Integration problem

    You seriously need to stop coding and start thinking about what you want to achieve. Random tinkering in the hope of eventually stumbling on an answer isn't going to cut it (now or in the long run).

    For example, list all the properties that ports and products have (eg. a name), and the relationship between a port and a set of products (for example). Then think about the kinds of operations (eg purchase from a port) you need to perform.

    Here's a random thing I just wrote
    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <time.h>
    
    #define NUM_PORTS     6
    #define NUM_PRODUCTS  9
    
    typedef struct products {
      char    name[20];
      int     numberAvailable;
      double  unitPrice;
    } products_p;
    
    typedef struct{
      char    portName[10];
    } ports_p;
    
    void showPorts ( ports_p *ports, int numPorts ) {
      int i;
      for ( i = 0 ; i < numPorts ; i++ ) {
        printf ( "%d : %s\n", i+1, ports[i].portName );
      }
    }
    
    void showProducts ( products_p *products, int numProducts ) {
      int i;
      printf ( "Product              Amount Price\n" );
      for ( i = 0 ; i < numProducts ; i++ ) {
        printf ( "%-20s %6d %4.2f\n",
                 products[i].name, products[i].numberAvailable, products[i].unitPrice );
      }
    }
    
    int readIntFromUser ( const char *prompt ) {
      char buff[BUFSIZ];
      fputs ( prompt, stdout );
      fflush ( stdout );
      if ( fgets ( buff, BUFSIZ, stdin ) != NULL ) {
        int result;
        if ( sscanf ( buff, "%d", &result ) == 1 ) {
          return result;
        } else {
          return 0;
        }
      } else {
        return 0;
      }
    }
    
    int purchase ( products_p *products, int numProducts, products_p *purchases ) {
      int i;
      int numPurchases = 0;
      for ( i = 0 ; i < numProducts ; i++ ) {
        int howMany;
        printf ( "%s : %d available at %f each\n",
                 products[i].name, products[i].numberAvailable, products[i].unitPrice );
        howMany = readIntFromUser ( "How many? > " );
        if ( howMany > 0 && howMany <= products[i].numberAvailable ) {
          /* reduce the port's amount by how many purchased */
          products[i].numberAvailable -= howMany;
    
          /* The number for us is how many we purchased */
          purchases[numPurchases] = products[i];
          purchases[numPurchases].numberAvailable = howMany;
          numPurchases++;
        }
      }
      return numPurchases;
    }
    
    int main( void ) {
      ports_p ports[NUM_PORTS] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
      /* This should be on a per-port basis */
      /* each port will have different products, at different prices */
      products_p products[NUM_PRODUCTS] = {
        { { "Fish" },     1, 11 },
        { { "Cotton" },   2, 12 },
        { { "Wood" },     3, 13 },
        { { "Coco" },     4, 14 },
        { { "Sugar" },    5, 15 },
        { { "Guns" },     6, 16 },
        { { "Textiles" }, 7, 17 },
        { { "Tobacco" },  8, 18 },
        { { "Wine" },     9, 19 },
      };
      int choice;
    
      showPorts ( ports, NUM_PORTS );
      choice = readIntFromUser ( "Choose a port > " );
    
      if ( choice >= 1 && choice <= NUM_PORTS ) {
        products_p purchases[NUM_PRODUCTS];   /* The list of products we bought */
        int numPurchases = 0;                 /* and how many products in all */
    
        printf ( "The port has the following available\n" );
        showProducts ( products, NUM_PRODUCTS );
    
        numPurchases = purchase ( products, NUM_PRODUCTS, purchases );
        printf ( "You purchased the following\n" );
        showProducts ( purchases, numPurchases );
    
        printf ( "The port has the following products left\n" );
        showProducts ( products, NUM_PRODUCTS );
      }
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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