Using functions

This is a discussion on Using functions within the C Programming forums, part of the General Programming Boards category; Hi guys, I'm learning how to do functions in my class now but i'm having a little trouble getting them ...

  1. #1
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17

    Using functions

    Hi guys, I'm learning how to do functions in my class now but i'm having a little trouble getting them to work.

    Code:
    void printFraction(int n, int d)
    {
         printf("%d/%d",n,d);
    }
    
    void scanFraction(int a, int b)
    {
         scanf("%d",&a);
         scanf("%d",&b);
    }
    
    char scanOperator(void)
    {
    
    }
    
    int main(void)
    
    {
        int a,b;
        printf("Enter the numerator and denominator of a common fraction: ");
        void scanFraction(int a,int b);
        printf("\n");
        if(b>0)
    {
        printf("You have entered: ");
        void printFraction(int a,int b);
    }
    else if(b<=0)
    {
        printf("The common fraction you entered is invalid.");
    }
    My first problem is just that It won't ask me to scan an integer, it skips the scanFraction functions and prints out the rest of my statements.
    Last edited by bloodshot772; 10-17-2010 at 04:10 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    That's because you never actually call the function. You only have a function prototype.
    To actually call a function, you need to use the syntax:
    name_of_function(name_of_paramter1, ...);
    Note that the type of the parameters shall NOT be included. Nor shall the return type of the function.
    You may wish to review this in your book.

    Also, << !! Posting Code? Read this First !! >>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Oh you're right.. I don't know what was going through my mind when I read the book tho, I'ts really hard for me to learn this stuff through text. I initially knew i didn't have to put (int a, int b) but I'm using Dev C++ and it kept telling me i should.

    So as for now I changed the "in main" function to scanFraction(a,b); is that correct? it seems to work.

    Thanks for the help, I will get back to you if I have any more trouble with this program

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    You may wish to improve indentation, as well.
    Also note that ScanFraction will not work properly without pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Hehe, yup I'm a bit sloppy. I don't quite understand pointers, reading up on that now.

  6. #6
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    So far so good, but I don't know what to do for this next part where i am asking the user to enter an arithmatic operator (+, -, *, /)...

    Code:
    void printFraction(int n, int d)
    {
         printf("%d/%d",n,d);
    }
    
    void scanFraction(int *n, int *p)
    {
         scanf("%d %d",&*n,&*p);
    }
    
    char scanOperator(void)
    {
         char c;
         scanf("%c",&c);
    }
    
    int main(void)
    
    {
        int a,b;
        char c;
        printf("Enter the numerator and denominator of a common fraction: ");
        scanFraction(&a,&b);
        for(;b<=0;)
    {   
        printf("The common fraction you entered is invalid.\n");
        printf("Enter the numerator and denominator of a common fraction: ");
        scanFraction(&a,&b);
    }
        printf("You have entered: ");
        printFraction(a,b);
        printf("\n");
        printf("Enter an arithmetic operator (+, -, *, /): ");
        scanOperator();
    system("PAUSE");
    return 0;
    }
    The output should look something like this:

    Enter an arithmetic operator (+, -, *, /): .<enter>
    The arithmetic operator you entered is invalid.
    Enter an arithmetic operator (+, -, *, /): *<enter>
    You have entered: *

    where the orange is user input.
    University of Toronto ECE 1T4

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  8. #8
    Registered User bloodshot772's Avatar
    Join Date
    Sep 2010
    Posts
    17
    Well guys I finished! Including the other parts to the program I didn't mention. Thanks for you're advise Elysia
    University of Toronto ECE 1T4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of functions
    By frktons in forum C Programming
    Replies: 29
    Last Post: 06-30-2010, 09:51 AM
  2. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  3. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  4. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  5. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM

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