# Can'nt figure out problem

• 12-23-2005
HAssan
Can'nt figure out problem
Hello

I wrote this small program that it askes for the user their name. After that the user has a option of going to any port but in this test it can only go to 1 single port. After the user has chosen there port a price list comes up with the products sold in the port. The prices for the product is generated by a ramdom number generator then multiplied by a number. But the program it's working I can'nt seem to be able to fix the mistakes. Can some one please point me out in the right direction.

Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define a 100 #define size 2 int variable(void); int Sydney(Goods_g *G, x[size]); typedef struct{         float Fish;         float Cotton;         }Goods_g; typedef struct{         char Sydney;         }Ports_p;         int main() {                 Goods_g G;         Goods_g *G;                 Ports_p P;         Ports_p *P;         char name[a];         int i;         int x;         x=1000;                 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");         return 0; } int variable(void) {   int i;   float x[size];   srand((unsigned)time(NULL));     for (i=0; i<2; i++)         x[i] = (rand()%26)/14.0f;      return 0; } int Sydney(Goods_g *G, x[size]) {         G->Fish=50*x[0];         G->Cotton=80*x[1];         printf("Welcome to Sydney\n");         printf("The prices for the material is as follows\n); printf("Fish=%f\n, G->Fish); printf("Cotton=%f\n, G->Cotton); return 0; }```
• 12-23-2005
kermit
Well, first up, you are not calling any of your own functions from main.

Here are some various thoughts:

Code:

`scanf("%s",&name);`
You will want to get rid of that 'address of' operator there (the &) - you just use the name of the array in that situation. Also, that method of reading input is about as dangerous as using gets. See the relevant FAQ's here and here. You may as well read this too. And if you must use scanf (though I would recommend using fgets and sscanf as per the FAQ's) you should know that it returns a value which corresponds to the number of items succesfully converted - so if you are looking for example, one int, then upon success, scanf would return a 1. You can test this to see if scanf was successful or not - if not, at least you have the option of cleaning up the mess. You can read more about the scanf family of functions here.

Next:

Code:

`if(i=1)`

Have a look at these statements:

Code:

```printf("The prices for the material is as follows\n); printf("Fish=%f\n, G->Fish); printf("Cotton=%f\n, G->Cotton);```
See anything wrong with them? Look closely.

Code:

`int Sydney(Goods_g * G, x[size]);`
What type is x? And do you know how to pass an array to a function?

Code:

```#define a 100 #define size 2```
Typically (though you are free to do as you have done here) #define'd constants use upper-case identifiers. This just makes it easier to identify them in the code. For example:

Code:

`#define SIZE 2`
That should get you started anyway.
• 12-23-2005
Salem
> if(i=1)
Try == for comparison
• 12-23-2005
dwks
Code:

`int Sydney(Goods_g *G, x[size]);`
You need a type for x.

Your variable function is kind of pointless. You need to keep it's result.
Code:

`printf("The prices for the material is as follows\n);`
You need a closing double quote:
Code:

`printf("The prices for the material is as follows\n");`
• 12-23-2005
dwks
Code:

```        Goods_g G;         Goods_g *G;```
And you're redeclaring the same variable here.
• 12-23-2005
quzah
Quote:

Originally Posted by HAssan
Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define a 100 #define size 2 int variable(void); int Sydney(Goods_g *G, x[size]); typedef struct{         float Fish;         float Cotton;         }Goods_g;```

There is no such type as 'Goods_g' at the time of your prototype. That type is only known after that, when you finally get around to creating it. I'm sure you're compiler is telling you this, so start paying attention to it.

Quzah.
• 12-26-2005
HAssan
Problems calling function
I Made All the modifications suggested but I'am having problems calling the function. I can'nt seen to make the function cal corectly. I want the array from the function variable to return it to function main so I can then pass it on to function Sydney.

Code:

``` #include <stdio.h> #include <stdlib.h> #include <time.h> #define a 100 #define size 9 typedef struct{         float Fish;         float Cotton; typedef struct{         char Sydney;         }Ports_p; float variable(void); int Sydney(Goods_g *G, float x[]);         int main() {                 Goods_g Goods;         Goods_g *G;         G= &Goods;                 Ports_p Ports;         Ports_p *P;         P= &Ports;         char name[a];         int i;         int x;         x=1000;         float y;                 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<0)               printf("You must enter 1\n"); y = variable();        Sydney(G,*y);         return 0; } float variable(void) {   int i;   float x[size];   srand((unsigned)time(NULL));     for (i=0; i<9; i++)         x[i] = (rand()%26)/14.0f;          return 0; } int Sydney(Goods_g *G, float x[]) {         G->Fish==50*x[0];         G->Cotton==80*x[1];                 printf("Welcome to Sydney\n");         printf("The prices for the material is as follows\n"); printf("Fish==%f\n, G->Fish\n"); printf("Cotton==%f\n, G->Cotton\n");  return 0; }```
• 12-26-2005
kermit
Code:

```float variable(void) {   int i;   float x[size];   srand((unsigned)time(NULL));     for (j=0; j<9; j++)         x[j] = (rand()%26)/14.0f;          return 0; }```
How do you ever expect to accomplish something when you are returning 0? What you have got here is that you are declaring some variables, and then when you are all done your calculations, you throw away the answer. Make your return statement return something meaningful. The way you have it, you return 0, which is stored in your variable y (that is a poor name for a variable) You need to return something meaningful from your function 'variable' and then go from there.

If you want to use the array from your function 'variable' you are going to have to do things differently. First of all, even if you did return 'x' from your function, you would not be able to use it, as it was declared locally in your function, and as soon as the function returns, its gone. Now there are ways around this, like declaring x as static, but that can get a little hairy too. Your best bet might be to declare the array 'f' (you really do need to use more descriptive variable names) inside of main - then pass it as a pointer to your function 'variable' - then you can populate it with any values you want, and then you have them available to pass on to your function Sydney.

Secondly, your function prototype for Sydney looks like this:

Code:

`int Sydney(Goods_g *G, float x[]);`
That tells the compiler to set up storage for a pointer of type Goods_g, and then a pointer of type float. But then when you call it, you give it this:

Code:

`Sydney(G,*y);`
Now the first argument you seem to have right, but the second one is not right at all. Let's assume that you declare f to be an array, and you have passed it to the function 'variable', which populates the array with values. Now you are ready to pass the array 'f' to the function Sydney. When you pass an array, you just use the name of the array, or alternatively the &array_name[0] notation, something like so:

Code:

```int main(void) {   float some_array[N]  /* where N is the number of elements for the array */     some_func(some_array);     /* or you could do */     some_func(&some_array[0]);     return 0; }```
edit::

I have done some writing on writing/using functions in C - there is nothing yet on using pointers and arrays, but for the general idea of functions, I think it might help make some things clearer for you. You can find it here.