Thread: The scanf function

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    The scanf function

    First of all, I'm new to C (having a course about embedded systems at a university in Denmark).

    I've a program which converts a BMP picture to grayscale and uses filters to create edge detection and some other fun stuff.

    I need to create something as simple as a menu like:

    Press an integer to pick a menupoint:
    1) Load image
    2) Modify image
    3) Save image

    When I use the following code I get a problem with the scanf. It pops up before anything else in my code, meaning when I run my program the scanf line is run before anything else even though it's nearly at the end.

    Code:
    printf("Welcome to DigiCam - Made by group 14, Andreas Hussing & Lars Holdgaard\n");
    printf("Please specify what you want:\n");
    writemenu();
    
    int opt = 0;
    scanf("%d",&opt);
    printf("\nOption chosen: %d\n\n", opt);
    I'm sure it's something quite basic I've misunderstood about the function, because when I Google my problem, I can't seem to find anyone else having this problem...

    Thanks!
    Can't wait to be able to help others with C too :-)

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    It seems scanf is getting called first ..could you post the whole code.. I mean your main function and when you are calling writemenu() function as well! .

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mcoroklo View Post
    When I use the following code I get a problem with the scanf. It pops up before anything else in my code, meaning when I run my program the scanf line is run before anything else even though it's nearly at the end.
    Why do you think that?
    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.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Quote Originally Posted by Elysia View Post
    Why do you think that?
    Because I ran the program and that was what happened! ;-)

    Here's my code:
    Code:
    /********************************************************************************
     * Test (main)
     ********************************************************************************/
    
    #include "ccd.h"
    #include "lcd.h"
    
    void writemenu()
    {
    	printf("Press 1 'PATH' to load an image\n");
    	printf("Press 2 'FILTEROPTION' to filter an image\n");
    	printf("Press 3 'PATH' to save an image\n ");
    }
    
    void performOption(int opt)
    {
    }
    
    int main(int argc, char *argv[]) {
    	printf("Welcome to DigiCam - Made by group 14, Andreas Hussing & Lars Holdgaard\n");
    	printf("Please specify what you want:\n");
    	writemenu();
    
    	int opt = 0;
    	scanf("%d",&opt);
    	printf("\nOption chosen: %d\n\n", opt);
    	performOption(opt);
    
                    return 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Most likely an orphaned newline being generated somewhere within 'writemenu', causing scanf to skip the input. Check out the FAQ on flushing the input buffer. If that doesn't help, I would suggest posting a complete example that demonstrates the problem.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Mcoroklo View Post
    Because I ran the program and that was what happened! ;-)

    Here's my code:
    Code:
    /********************************************************************************
     * Test (main)
     ********************************************************************************/
    
    #include "ccd.h"
    #include "lcd.h"
    
    void writemenu()
    {
        printf("Press 1 'PATH' to load an image\n");
        printf("Press 2 'FILTEROPTION' to filter an image\n");
        printf("Press 3 'PATH' to save an image\n ");
    }
    
    void performOption(int opt)
    {
    }
    
    int main(int argc, char *argv[]) {
        printf("Welcome to DigiCam - Made by group 14, Andreas Hussing & Lars Holdgaard\n");
        printf("Please specify what you want:\n");
        writemenu();
    
        int opt = 0;
        scanf("%d",&opt);
        printf("\nOption chosen: %d\n\n", opt);
        performOption(opt);
    
                    return 0;
    }
    Sorry, I may have misunderstood your question. Some wonky compilers don't flush the output buffer correctly, in which case you might need to add a fflush( stdout ) after the print statements...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM