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.