Can'nt figure out problem

This is a discussion on Can'nt figure out problem within the C Programming forums, part of the General Programming Boards category; Hello I wrote this small program that it askes for the user their name. After that the user has a ...

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

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

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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)
    You made a typo there. Try == instead.

    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.
    Last edited by kermit; 12-23-2005 at 02:57 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > if(i=1)
    Try == for comparison

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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");
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    	Goods_g G;
    	Goods_g *G;
    And you're redeclaring the same variable here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

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

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

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    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.
    Last edited by kermit; 12-26-2005 at 08:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21