Thread: Just a few minor mistakes

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

    Just a few minor mistakes

    Hello I wrote this program there is a mistake on lune 54 but I figure out what it is. I tried to fix it but without any luck. THe program askes the user for an input then it ask's the user to select a port that they want to go to (In this case the port will always be sydney".
    Then it prints out the list of materials it sells in Sydney and their prices.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define A 100
    #define SIZE 2
    
    typedef struct{
    	float Fish;
    	float Cotton;
    	
    }Goods_g;
    
    typedef struct{
    	char Sydney;
    	
    }Ports_p;
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[SIZE]);	
    
    int main()
    {
    	
    	
    	Goods_g *G; 
    	
                 Ports_p *P;
    
    	char name[A];
    	int i;
    	int x=1000;
    	float variables;
    	int j;
    	variables[SIZE];
    	
    printf("Please enter your name: ");
    	scanf("%s",&name);
    	
    printf("Please select which pont you want to start in: ");
    	scanf("%d",&i);
    	
    if(i==1)
    	printf("Welcome to Sydney\n");
    	
    if(i>1 || i<1)
    printf(" you must enter 1\n");
    
    
       
    srand((unsigned)time(NULL));
         for (j=0; j<2; j++)
            variables[j] = (rand()%26)/14.0f;
            
    Sydney(G, P, variables[]);                    //Line 54 the mistake is in this line but i can'nt figure out what it is.
        
     return 0;
    }
    	
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[SIZE])
    {
    	G->Fish==50*variables[0];
    	G->Cotton==80*variables[1];
    
    	printf("Welcome to %s\n",P->Sydney);
    	
    printf("The prices for the material is as follows\n");
    
    printf("Fish=%f\n", G->Fish);
    printf("Cotton=%f\n", G->Cotton);
    
    return 0;
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, I get the error:
    Code:
    error: parse error before ']' token
    If you want to pass an array to a function, you just pass it without any [ or ].

    Also you have another suspect line:
    Code:
    variables[SIZE];
    What is this supposed to be? It's not a declaration, you already declared "float variables" above. Did you mean to declare this as an array? If so:
    Code:
    float variables[SIZE];

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

    Program keeps crashingh

    Hello I made the changes suggested the program works upto the point where it has to display the prices of the material. But as it is meant to display the price of the material the program just crashes why is that happeningany suggestions.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define A 100
    #define SIZE 2
    
    typedef struct{
    	float Fish;
    	float Cotton;
    	
    }Goods_g;
    
    typedef struct{
    	char Sydney;
    	
    }Ports_p;
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[]);	
    
    int main()
    {
    	
    	
    	Goods_g *G; 
    	
                Ports_p *P;
    
    	char name[A];
    	int i;
    	int x=1000;
    	int j;
                 float variables[SIZE];
    	
    printf("Please enter your name: ");
    	scanf("%s",&name);
    	
    printf("Please select which pont you want to start in: ");
    	scanf("%d",&i);
    	
    if(i==1)
    	printf("Welcome to Sydney\n");
    	
    if(i>1 || i<1)
    printf(" you must enter 1\n");
    
    
       
    srand((unsigned)time(NULL));
         for (j=0; j<2; j++)
            variables[j] = (rand()%26)/14.0f;
            
    Sydney(G, P, variables);
        
     return 0;
    }
    	
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[])
    {
    	G->Fish==50*variables[0];
    	G->Cotton==80*variables[1];
    
    	printf("Welcome to %s\n",P->Sydney);
    	
    printf("The prices for the material is as follows\n");
    
    printf("Fish=%f\n", G->Fish);
    printf("Cotton=%f\n", G->Cotton);
    
    return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    #define A 100
    #define SIZE 2
    
    typedef struct{
      float Fish;
      float Cotton;
    }Goods_g;
    
    typedef struct{
      char Sydney[10];  /*!! you need more that a char for a string */
    }Ports_p;
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[]);
    
    int main()
    {
      Goods_g G;
      Ports_p P;        /*!! note - NOT POINTERS here */
    
      char name[A];
      int i;
      int x=1000;       /*!! this is UNUSED */
      int j;
      float variables[SIZE];
    
      printf("Please enter your name: ");
      scanf("%s",name); /*!! No & here, name is an array */
    
      printf("Please select which pont you want to start in: ");
      scanf("%d",&i);
    
      if(i==1)
        printf("Welcome to Sydney\n");
    
      if(i>1 || i<1)
        printf(" you must enter 1\n");
    
      srand((unsigned)time(NULL));
      for (j=0; j<2; j++)
        variables[j] = (rand()%26)/14.0f;
      strcpy( P.Sydney, "Sydney" );
    
      Sydney(&G, &P, variables);  /*!! Note - make POINTERS here */
    
      return 0;
    }
    
    
    int Sydney(Goods_g *G, Ports_p *P, float variables[])
    {
      G->Fish   = 50*variables[0];     /*!! you used == here, you meant = */
      G->Cotton = 80*variables[1];
    
      printf("Welcome to %s\n",P->Sydney);
    
      printf("The prices for the material is as follows\n");
    
      printf("Fish=%f\n", G->Fish);
      printf("Cotton=%f\n", G->Cotton);
    
      return 0;
    }

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

    Thanks

    Thanks it is working now

Popular pages Recent additions subscribe to a feed