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;
}