Thread: Integration problem

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

    Integration problem

    Hello I wrote this program it askes the user for an input their name then it ask's the user to select a port that they want to go to. Then it prints out the list of materials it sells in that port. Then the user has $1000 of initial money he/she can pick which material they want to buy. But the problem is that the function purchase is not working. I would appriciate it if some one can point out my mistakes and seggest ideas on ways how I can solve this problem.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 9
    
    typedef struct{
      float Fish;
      float Cotton;
      float Wood;
      float Coco;
      float Sugar;
      float Guns;
      float Textiles;
      float Tobacco;
      float Wine;
    }Goods_g;
    
    typedef struct{
      char PortName[10];
    }Ports_p;
    
    int DisplayPort(Goods_g *G, Ports_p *P, float variables[]);
    
    int Purchase(float variables[], float money);
    
    int main()
    {
      Goods_g G;
      Ports_p P[6] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
      char name[A];
      int i;
      int j;
      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, &P[i-1], variables);
      }
      
    Purchase(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(float variables[], float money)
    {
      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;
    	int i;
    	
    printf("Which good do you wish to buy: ");
    scanf("%d", &i);
    
    if(i==1)
    {
    
    printf("How many individual units of fish do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals = G->Fish*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of fish\n",amount);
                 }
    
    }
    
    if(i==2)
    {
    	
    printf("How many individual units of cotton do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Cotton*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of cotton\n",amount);
                 }
                 
    }
    
    if(i==3)
    {
    	
    printf("How many individual units of wood do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Wood*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of wood\n",amount);
                 }
                 
    }
    
    
    if(i==4)
    {
    	
    printf("How many individual units of coco do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Coco*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of coco\n",amount);
                 }
                 
    }
    
    if(i==5)
    {
    	
    printf("How many individual units of sugar do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Sugar*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of sugar\n",amount);
                 }
                 
    }
    
    if(i==6)
    {
    	
    printf("How many individual units of guns do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Guns*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of guns\n",amount);
                 }
                 
    }
    
    if(i==7)
    {
    	
    printf("How many individual units of textiles do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Textiles*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of textiles\n",amount);
                 }
                 
    }
    
    if(i==8)
    {
    	
    printf("How many individual units of tobacco do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Tobacco*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of tobacco\n",amount);
                 }
                 
    }
    
    if(i==9)
    {
    	
    printf("How many individual units of wine do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Wine*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of wine\n",amount);
                 }
                 
    }
    
    return 0;
    }

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    i said that i'd give it a go and try and help you and i found that your code wont compile, the problem with the purchases fuction is that the struct "G" is reffered to in the function, but it is defined in main only. i changed the prototype, fuction call and function definition to accomodate this and i think its working. here is your code again, with the modifications i made:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 9
    
    typedef struct{
      float Fish;
      float Cotton;
      float Wood;
      float Coco;
      float Sugar;
      float Guns;
      float Textiles;
      float Tobacco;
      float Wine;
    }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;
      Ports_p P[6] = {
        { "Sydney" },
        { "London" },
        { "LosAngles" },
        { "NewYork" },
        { "Paris" },
        { "Tokyo" },
      };
      char name[A];
      int i;
      int j;
      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, &P[i-1], variables);
      }
      
    Purchase(&G, 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;
    	int i;
    	
    printf("Which good do you wish to buy: ");
    scanf("%d", &i);
    
    if(i==1)
    {
    
    printf("How many individual units of fish do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals = G->Fish*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of fish\n",amount);
                 }
    
    }
    
    if(i==2)
    {
    	
    printf("How many individual units of cotton do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Cotton*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of cotton\n",amount);
                 }
                 
    }
    
    if(i==3)
    {
    	
    printf("How many individual units of wood do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Wood*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of wood\n",amount);
                 }
                 
    }
    
    
    if(i==4)
    {
    	
    printf("How many individual units of coco do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Coco*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of coco\n",amount);
                 }
                 
    }
    
    if(i==5)
    {
    	
    printf("How many individual units of sugar do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Sugar*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of sugar\n",amount);
                 }
                 
    }
    
    if(i==6)
    {
    	
    printf("How many individual units of guns do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Guns*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of guns\n",amount);
                 }
                 
    }
    
    if(i==7)
    {
    	
    printf("How many individual units of textiles do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Textiles*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of textiles\n",amount);
                 }
                 
    }
    
    if(i==8)
    {
    	
    printf("How many individual units of tobacco do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Tobacco*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of tobacco\n",amount);
                 }
                 
    }
    
    if(i==9)
    {
    	
    printf("How many individual units of wine do you wish to buy: ");
    scanf("%d", &amount);
    
    if (amount != -1) {
    
                 totals =G->Wine*amount;
                 totalm = money-totals;
                 printf("You have %f money left\n", totalm);
                 printf("You have %d unit(s) of wine\n",amount);
                 }
                 
    }
    system ("pause"); /*Just so you can see the program's output*/
    return 0;
    }

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

    Thanks

    It worked thanks for that. I finally realised I did'nt pass the structure into function purchase. But just 1 I had is that what does the line below do.

    Code:
    system ("pause");

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > snip code
    Code:
    typedef struct{
      float Fish;
      float Cotton;
      float Wood;
      float Coco;
      float Sugar;
      float Guns;
      float Textiles;
      float Tobacco;
      float Wine;
    }Goods_g;
    You're still not thinking about the wider issues here. I managed to wean you off naming every single variable for a different port, and you've gone and done exactly the same thing for the products for sale!

    Code:
    struct products {
        char name[20];
        int   numberAvailable;
        double unitPrice;
    } products_p;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with for loop calling external function
    By lordbubonicus in forum C Programming
    Replies: 2
    Last Post: 10-13-2007, 10:54 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Integration Problem
    By Tarento in forum C Programming
    Replies: 5
    Last Post: 05-09-2006, 12:48 AM