Creating an scanf function inside an function

This is a discussion on Creating an scanf function inside an function within the C Programming forums, part of the General Programming Boards category; Hello, I've an problem whit understanding why i can't make an scanf() work inside an custom function. This is the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Question Creating an scanf function inside an function

    Hello,

    I've an problem whit understanding why i can't make an scanf() work inside an custom function.

    This is the programing:

    PHP Code:
    int main(int argcchar** argv) {

        
    int scan();
        return (
    EXIT_SUCCESS);
    }

    int scan(){
        
    int number;
        
    scanf("%d", &number);
        
    printf("%d\n"number);

    As you can see i've made main call the scan function which should promote the user to type in but it just skips it. The program works if i just make the scanf inside of main but not when it's called via an function.

    How should i do in order to get this to work?

    Thank you for your time.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
        int scan();
    is not a function call. It's called function prototype.
    You call a function by its name and arguments if there is any.

    like
    Code:
      scan();
      x = sin( 3.0 );
      printf(" sin(%f) = %f\n",3.0, x);
    You are already calling scanf() and printf().....

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Thank you for your replay,

    Call me stupid but i do not really comprehend whit your answer.
    What should i do in order to actually get this to work?

    You typed the following:

    x = sin( 3.0 );
    printf(" sin(%f) = %f\n",3.0, x);

    but i don't really understand what i should do whit it.
    Could you please make me understand the bigger picture of it all?

    The whole point of this is that we got this assignment we need to complete and i guess i perhaps have misunderstood the whole concept of it. This is what we have to create:

    We are going to use an function called flashin which will then take an float and print it out.

    float flashin(char text[])

    This is an example description on what the program should include:

    #include <stdio.h>
    #include <stdlib.h>

    ... The function flashin() here

    int main(void)
    {
    float onenumber ;

    onenumber = flasin(”Type in an decimal number : ”);

    printf(”You typed %5.2f\n”, onenumber );

    system(”pause”);
    return 0;
    }

    My first guess was to integrate whit scanf() or gets() inside of the function but i might be wrong. Could someone be so kind to explain how i can make this work.
    Last edited by anserudd; 03-25-2011 at 09:08 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anserudd View Post
    Hello,

    I've an problem whit understanding why i can't make an scanf() work inside an custom function.
    A few problems:

    1) Since you are not using function prototypes you need to place your scan() function above main. This is so that by the time the compiler reads down to main() it already knows about scan().

    2) when calling a function just use it's name. That is if you've defined int scan() then you call it simply by typing scan(); ... don't repeat the entire declaration. (hint: you are already doing this with scanf() and printf() just fine.)

    3) the correct declaration for a function that does not take any input values is ... function(void) ... not function() ... the latter says "accepts an unknown number of parameters" and that's not what you are doing.

    4) the correct declaration for a function that does not return a value is ... void function() ... if you declare it as int function(void), the compiler expects a returned value, which you aren't providing.

    5) Since you aren't (yet) making any use of command line arguments the correct form for your main function is int main (void) ... that may change if you add stuff to your program.

    Soooo...

    Code:
    void scan( void ){
        int number;
        scanf("%d", &number);
        printf("%d\n", number);
    }
    
    int main( void ) {
    
        scan();
        return (EXIT_SUCCESS);
    }

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    When you call a function you have to supply a number of things (taking sin as an example):
    Code:
    double sin ( double arg ); /* sin's prototype */
    
    /* function call a.k.a. function invocation */
    double x;
    x = sin( 3.0 );
    Color coordinated for your convenience. Your scan() function would be similar, it is only the type and the number of arguments that are different. Once you understand how to call a function, you'll see problems with how you invoke scan. Currently, you specify it will return an int, but there is no place for an int to be stored where you call scan.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  2. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 09:01 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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