Thread: Passing Value back problem

  1. #1
    Unregistered
    Guest

    Unhappy Passing Value back problem

    My program asks the user to input sold and price. both are floting point number. they use the same function for validation. the function checks the input data which must be 0-9 and/or "."
    i use "pass" for checking true or false. it is supposed to send the value of "pass" to the main but it isn't
    how can i fix this problem?
    Thanks
    Code:
    #include <stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    void check(struct Sell s,char amoun[50]);
    struct Sell 
    {
    	float amountsold,retailprice;
    	bool pass;
    };
    //******************M a i n*************
    	
     int main  (void)
    
    {
    struct Sell st;
    bool k=true;
    char amountsol[50],retailprice[50];
    int e,len,i;
    st.pass=true;
    	do
    	{
    			
    			
    		do
    		{
    		    puts("Enter  amount sold: ");
    		    gets(amountsol);
    		    check(st,amountsol);
    		    printf("Pass Main:%d\n",st.pass);
    		}while(!st.pass);
    	 st.amountsold=atof(amountsol);
    	printf("Amoundsold :%f\n",st.amountsold);
    		do
    		{
    		puts("Enter retailprice: ");
    		gets(retailprice);
    		check(st,retailprice);
    		printf("Pass Main :%d\n",st.pass);
    		}while(!st.pass);
    	st.retailprice=atof(retailprice);
    	} while(!st.pass);
    	
    	printf("Amountsold :%f \n",st.amountsold);
    	printf("amountinFunct: %f\n",st.retailprice);
    }
    //**********f u n c t i o n ********************
    void check(struct Sell s,char num[50])
    {
    			
    int e,len,i,n;
    len=strlen(num);
    for (e=0;e<len;e++)
             {
    	switch (num[e])
                  {
    					
    	case '0' : 
                     	case '1' :
                     	case '2' :
                      	case '3' :
                      	case '4' :
                      	case '5' :
                        case '6' :
                      	case '7' :
                      	case '8' :
                      	case '9' :
                      	case '.' :
                      	s.pass=true;
                      	printf("Pass Funct:%d\n",s.pass);
                      	break;
                      	default : 
                        s.pass=false;
                        printf("Pass Funct:%d\n",s.pass);
                        puts(" Amount sold error \n");
               	break;
               	}
               }
        
               printf("Pass in Function before return :%d\n",s.pass);
    return(s.pass);
    }

  2. #2
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    if im not wrong, which i could well be, the problem is in your function declaration.

    void function(params)

    that means it wont pass anything back. wat u want is probably this:

    bool function(params)

    which indicates itll return a bool, or int as main does "int main(), etc.

    im relatively new to c but i think this is right P

  3. #3
    Unregistered
    Guest

    Unhappy

    it doesn't seem to be working
    any suggestion?
    cheers

  4. #4
    Registered User methodmza's Avatar
    Join Date
    May 2002
    Posts
    8
    another thing u might try
    since Check returns s.pass...it wont modify s.pass in the function that calls it.
    this will:
    s.pass = check....

    as s.pass becomes the returned value

    or

    if(check(...))

    etc.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    it doesn't seem to be working
    any suggestion?
    cheers
    Did you remember to change both the prototype and the function return specs:

    >bool check(struct Sell s, char amoun[50]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest
    Hammer
    i tried it but nothing change :-(

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's the output I get from you prog after making a few changes. Can you go through this and verify it:
    Enter amount sold:
    10
    Pass Funct:1
    Pass Funct:1
    Pass in Function before return :1
    Pass Main:1
    Amoundsold :10.000000
    Enter retailprice:
    100
    Pass Funct:1
    Pass Funct:1
    Pass Funct:1
    Pass in Function before return :1
    Pass Main :1
    Amountsold :10.000000
    amountinFunct: 100.000000
    If it's correct, all I did was

    - Change return type on function check() from void to bool.
    - Moved the structure definition to before the function check()'s prototype. This is because you were using it before defining it.
    - I also had to typedef an int to bool, and using #define's for true/false as my compiler doesn't recognise them.

    Let me know how you get on.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Unregistered
    Guest
    Thanks Hammer
    just woke up
    i am quite new with C
    i did what u told me except the last one
    "- I also had to typedef an int to bool, and using #define's for true/false as my compiler doesn't recognise them. "
    i don't know how to do it
    the point of this program is when u put any character in to the number, for example 12.a ,2w,s, 150.y etc,it should show an error message and ask the same question for u to put the number again
    thanks

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I see your next problem (where I come from they're called challenges, not problems!).

    The struct within main() is passed to the function check(). This function then has it's own copy of the struct to play with, and alter as it sees fit. Any changes made to the struct within check() do not affect the struct in main() simply because they are 2 seperate variables.

    Also, you return a value from check(), but you never assign it to anything in main(). I think a better example of using check() would be like this
    Code:
    st.pass = check(amountsol);
    if (st.pass)
    /* etc etc */
    You don't have to do it that way, it's up to you. Whatever you do though will require a few tweaks in your code. Have another try and post here again when you're ready.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always do:
    Code:
    struct mystruct myfunction( struct mystruct x )
    {
        struct mystruct y;
        y = x;
        ... do stuff to y ...
        return y;
    }
    Then call it:

    struct mystruct someVar;

    someVar = myfunction( someVar );

    There's a way of changing a value without passing it as a pointer. Basicly pass it by value, and then modify the original with the return-by-value.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Unregistered
    Guest
    thanks you guys
    i solve this problem
    actually it was my mistake i was just confused
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-11-2008, 02:42 AM
  2. Replies: 1
    Last Post: 03-31-2008, 05:53 PM
  3. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. A problem with passing.
    By Bartleby in forum C++ Programming
    Replies: 2
    Last Post: 12-06-2002, 04:26 PM