Thread: Function Pass Issue

  1. #1
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96

    Function Pass Issue

    Please take a VERY close look at this (especially the bold parts):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void selection();	
    
    
    
    void selection()
    {
        int choice = 0;
        char number1[50];
        char number2[50];
        char prechoice[50];
        float n = 0.0;
        float n2 = 0.0;
    
        printf("Enter the two numbers IN ORDER e.g. 5 then 2 for 5-2\n");
        printf("Enter the first number: ");
    	if (fgets(number1, sizeof(number1), stdin) != NULL)
      	{
    		printf("%s is number1\n",number1);
        		n = atof(number1);
    		printf("%f is n\n",n);
      	}  
        printf("\nAnd now the second number: ");
    	if (fgets(number2, sizeof(number2), stdin) != NULL)
      	{
    		printf("%s is number2\n",number2);
        		n2 = atof(number2);
    		printf("%f is n2\n",n2);
      	}  
    
        printf("\n\nNow type in \'1\' for addition, \'2\' for subtraction,");
        puts(" \'3\' for division, \'4\' for multiplication, and \'5\' for power");
        printf("Please enter your choice: ");
    	if (fgets(prechoice, sizeof(prechoice), stdin) != NULL)
      	{
    	choice = atoi(prechoice);
    		printf("%d is choice\n",choice);
      	}  
    
        switch (choice) {
        case 1:
    	puts("Calculating...\n");
    	printf("%f is n\n",n);
    	printf("%f is n2\n",n2);
    	addition(n, n2);
    	break;
        case 2:
    	puts("Calculating...\n");
    	subtraction(n, n2);
    	break;
        case 3:
    	puts("Calculating...\n");
    	division(n, n2);
    	break;
        case 4:
    	puts("Calculating...\n");
    	multiplication(n, n2);
    	break;
        case 5:
    	puts("Calculating...\n");
    	power(n, n2);
    	break;
        default:
    	puts("Please enter one of the operations listed ONLY");
    	menu();
        }
    }
    Pretend we are only working with addition here, to get this problem solved. See how I stuck a bunch of printf's in there? Well, this was to show me that the variables are actually carrying the correct values. Which is the case. Now look at this snippet from my header file:
    Code:
    void addition(float number1,float number2);
    void addition(float number1,float number2)
    {
    	float answer;
    	answer = number1+number2;
    	printf("%f + %f = %f\n",number1,number2,answer);
    	menu();
    }
    I enter 5 for the first number and 9 for the second. According to the code above (which I tested by running the .exe) I get this:
    Enter two numbers IN ORDER e.g. 5 then 2 for 5-2
    Enter the first number: 5
    5
    is number1
    5.000000 is n

    And now the second number: 9
    9
    is number2
    9.000000 is n2
    (Note from Padawan: Everything is fine see?)




    Now type in '1' for addition, '2' for subtraction, '3' for division, '4' for multiplication, and '5' for power
    Please enter your choice: 1
    1 is choice
    Calculating...
    5.000000 is n
    9.000000 is n2
    (Note from Padawan: Notice how the variables are STILL FINE)
    (Cont. Note: These variables are now passed to the addition function)
    0.000000 + 2.37500 = 2.37500
    (Note from Padawan: I have noooooo clue where that came from, do you?)
    As you can see, something odd is going on in my code once again. This time it appears to be something with the passing of the variables to the function. ButI have no clue what. Can someone help me out here? I'd appreciate it more than you can possibly imagine because it's now been a full 24 hours since I started this simple calculator project.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, don't spam the board with your post.
    For finnishers:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void addition(float number1,float number2)
    {
                    float answer;
                            answer = number1+number2;
                                    printf("%f + %f = %f\n",number1,number2,answer);
    }
    
    void selection()
    {
            int choice = 0;
            char number1[50];
            char number2[50];
            char prechoice[50];
            float n = 0.0;
            float n2 = 0.0;
    
            printf("Enter the two numbers IN ORDER e.g. 5 then 2 for 5-2\n");
            printf("Enter the first number: ");
            if (fgets(number1, sizeof(number1), stdin) != NULL)
            {
                    printf("%s is number1\n",number1);
                    n = atof(number1);
                    printf("%f is n\n",n);
            }
    
            printf("\nAnd now the second number: ");
            if (fgets(number2, sizeof(number2), stdin) != NULL)
            {
                    printf("%s is number2\n",number2);
                    n2 = atof(number2);
                    printf("%f is n2\n",n2);
            }
    
            printf("\n\nNow type in \'1\' for addition, \'2\' for subtraction,");
            puts(" \'3\' for division, \'4\' for multiplication, and \'5\' for power");
            printf("Please enter your choice: ");
            if (fgets(prechoice, sizeof(prechoice), stdin) != NULL)
            {
                    choice = atoi(prechoice);
                    printf("%d is choice\n",choice);
            }
    
            switch (choice)
            {
                    case 1:
                            puts("Calculating...\n");
                            printf("%f is n\n",n);
                            printf("%f is n2\n",n2);
                            addition(n, n2);
                            break;
            }
    }
    
    int main( void )
    {
            selection( );
            return 0;
    }
    ...it works fine for me. One thing you will need to notice here though is that all of your if checks do stuff if they're correct, but if it's incorrect, it just continues merrily along as if nothing were wrong. That's usually a BadThingTM.

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

  3. #3
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    Hmmmm. Must be something with the linking together of my multiple source files.

    For starters, don't spam the board with your post.
    I did not spam the board. I deleted my other post in my older thread and replaced it with this new thread because it is another issue.

    And I will fix those if statements. I was just trying to get this other thing squared away first. Thanks for your support.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    -deleted-
    Last edited by nonpuz; 04-04-2004 at 11:32 PM.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    heh i guess quizah beat me too it.

  6. #6
    Novice C++ Programmer
    Join Date
    Nov 2003
    Posts
    96
    This is very odd. Now I tried moving all my functions into one file and the compiler says "subtraction was implicitly declared to return int" even though all appearances of subtraction() in my program are of type float.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That means you're trying to use it before it's been prototyped or defined.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM