Problem With Code

This is a discussion on Problem With Code within the C Programming forums, part of the General Programming Boards category; Hello I'am trying to write a program that allows the user to buy and sell products in a port then ...

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

    Problem With Code

    Hello I'am trying to write a program that allows the user to buy and sell products in a port then go to another port and buy and sell products. But there is a problem in the pruchase function. I can'nt figure out what the problem is so I would appriciate any help anyone can give me.

    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); 
    
    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); 
    
      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) 
    {
      G[0]->Goods_g   = 50*variables[0];
      G[1]->Goods_g = 80*variables[1];
      G[2]->Goods_g = 100*variables[2];
      G[3]->Goods_g = 140*variables[3];
      G[4]->Goods_g = 350*variables[4];
      G[5]->Goods_g = 700*variables[5];
      G[6]->Goods_g =1350*variables[6];
      G[7]->Goods_g =1900*variables[7];
      G[8]->Goods_g =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;
    	}

    Code:
    Dear Salem if you are reading this I want to you to know that I did think about how each function should work and what it should do but it's just that I rather get things done 1 at a time rather then doing everything together. Plus thank you for all your help so far.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What is the "problem"? Does it compile? Does it crash when you enter "hippo" instead of "123"? Does it crash all the time? Have you tried debugging it?

    Part of your problem may be the fact that k is uninitialized in main(). Turn on compiler warnings!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 10:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 05:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 11:53 PM

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