Thread: make function?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    make function?

    Hello everybody, I am reading a book, and there is a part that tells you how to make functions. I can only make super simple arithmetic ones, like one that will add or subtract two parameters. But ive been trying to make one where there is only one parameter, and a getchar is supposed to be converted from farenheit to celcius. It doesnt work, as when ran, and 32 is input, it comes up with 10, where it should come out with 0. What is wrong with the code?

    Code:
    #include <stdio.h>
    
    int degree(int m);
    
    /* print Fahrenheit-Celsius table
    	for fahr = 0, 20, ..., 300 */
    main(){
    	int c;
    	c = getchar();
    	printf("%d", degree(c));
     	}
    
    	int degree( int base)
    	{
    		int  p;
    		
    		p = 5 * (base-32) /9;
    		return p;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Christ almighty... I am not at my computer, thus I have no compiler ergo I rewrote your code in JavaScript and had no troubles... Then I saw your problem.

    Code:
    #include <stdio.h>
    
    int degree(int m);
    
    /* print Fahrenheit-Celsius table
    	for fahr = 0, 20, ..., 300 */
    int main(void){
    	int c;
    	c = getchar();
    	printf("&#37;d", degree(c));
                    return 0;
     }
    
    int degree( int base)
    {
    	int  p;
    
    	p = 5 * (base-32) /9;
    	return p;
    }
    Try using scanf("%d", &c) instead.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    I know that there are other ways to do it, but i mean, why doesnt it work when i creat a function?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Look at the red bolded line, bud. Its not your function that is the problem, it was how you were reading input.

    Example:
    Code:
    #include <stdio.h>
    
    int degree(int m);
    
    /* print Fahrenheit-Celsius table
        for fahr = 0, 20, ..., 300 */
    
    int main(void){
        int c;
    
        // c = getchar();
        /* Why does this not work? Because lets say you input the number 5.
         * "5" has an ascii value of 53. So c will hold the value 53 instead of 5.
         */
    
        scanf("&#37;d", &c);
        /* Why does this one work? Because if you type in "535" it will retain the value
         * 535, not the character value in the input buffer.
         */
    
        printf("%d", degree(c));
        return 0;
     }
    
    int degree( int base)
    {
        int  p;
        p = 5 * (base-32) /9;
        return p;
    }
    Your function was fine. Its how you were accepting your input text that was erroneous.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The function is irrelevant here. If you turned this:
    Code:
    #include <stdio.h>
    
    int degree(int m);
    
    /* print Fahrenheit-Celsius table
    	for fahr = 0, 20, ..., 300 */
    main(){
    	int c;
    	c = getchar();
    	printf("&#37;d", degree(c));
     	}
    
    	int degree( int base)
    	{
    		int  p;
    		
    		p = 5 * (base-32) /9;
    		return p;
    }
    into this:
    Code:
    #include <stdio.h>
    
    int degree(int m);
    
    /* print Fahrenheit-Celsius table
    	for fahr = 0, 20, ..., 300 */
    main(){
    	int c;
    	c = getchar();
    	int  p;
    	p = 5 * (c-32) /9;
    	printf("%d", p);
     	}
    that would also fail to work. The problem is getchar() -- which doesn't accept numerical input as numbers. When you type 32, getchar only gets the 3, and then converts it to its ASCII value which is 50 and passes that to the function (which correctly converts it to 10 celsius).
    Last edited by tabstop; 08-20-2008 at 07:26 PM. Reason: Incomplete conversion

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    51, but we still love you tabstop.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    5
    Ok, thanks. Well the point of my project is making functions, anybody got any good ideas for one for me to practice on?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by D3V1LD0G View Post
    Ok, thanks. Well the point of my project is making functions, anybody got any good ideas for one for me to practice on?
    Pick your favorite library function from string.h and try to implement it. (No fair calling the real library function, of course.)

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I think a beneficial side-lesson you should have learned during your experience with this program is not only that you can make functions, but its important to know how to use functions written by others (i.e. the C standard library function, getchar()). You should also try to use your debugger when things don't look right. Or if you are doing something small like this, you could even go ahead and put printf's all over the place. But depending on your IDE, the debugger should be the easiest to implement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM