Thread: Issues with the scanf Function

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Issues with the scanf Function

    I am working on a code for my C programming class, I am completely new to C programming. Everytime I follow all of the guidelines and copy exactly what the book examples are or an online tutorial says the scanf function returns gibberish numbers or the number 0. This is my code, can anyone help me figure out what it is that I am doing wrong please?

    Code:
    #include <stdio.h>
    main(void)
    {
    /* Declares variables */
    int a, b, f, x, y;
    /* Initializes variables */
    a = 0;
    b = 0;
    f = 0;
    x = 0;
    y = 0;
    /* Asks the user for their input to determine the values of the variables a, b, x, and y */
    printf("\nEnter the value for the variable a: ");
    scanf("%d", &a);
    printf("\nEnter the value for the variable b: ");
    scanf("%d", &b);
    printf("\nEnter the value for the variable x: ");
    scanf("%d", &x);
    printf("\nEnter the value for the variable y: ");
    scanf("%d", &y);
    f = ((a - b)*(x - y));
    printf("%d", f);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks okay to me. The changes that I would make are:
    • Explicitly declare the return type of main to be int.
    • Indent the code.
    • print f with a trailing newline for readability.
    • Add a return statement to main, which is optional as a special case.

    Thus:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        /* Declares variables */
        int a, b, f, x, y;
        /* Initializes variables */
        a = 0;
        b = 0;
        f = 0;
        x = 0;
        y = 0;
        /* Asks the user for their input to determine the values of the variables a, b, x, and y */
        printf("\nEnter the value for the variable a: ");
        scanf("%d", &a);
        printf("\nEnter the value for the variable b: ");
        scanf("%d", &b);
        printf("\nEnter the value for the variable x: ");
        scanf("%d", &x);
        printf("\nEnter the value for the variable y: ");
        scanf("%d", &y);
        f = ((a - b)*(x - y));
        printf("%d\n", f);
        return 0;
    }
    You should also check the return value of scanf, but for a beginner program or test program that can be skipped.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    I tried the code you gave me

    I am using the cygwin compiler and it does not let me input the values even after I tried the code you gave me. For some reason it still outputs 0 after the last prompt.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Hold on, what is "it"? The compiler? You should be running the program from the command prompt.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It works fine for me with cygwin.
    Code:
    gcc -o fiveints.c fiveints -Wall -pedantic
    ./fiveints
    Enter the value for the variable a: 1
    
    Enter the value for the variable b: 2
    
    Enter the value for the variable x: 3
    
    Enter the value for the variable y: 4
    1

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

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    Compiler

    Quote Originally Posted by laserlight View Post
    Hold on, what is "it"? The compiler? You should be running the program from the command prompt.
    It's the Cygwin environment that I am running the code through. The compiler I am using is gcc. This is the output of the code that I get when I run the code that you gave me:

    Code:
    ~
    $ ./a.exe
    Enter the value for the variable a:
    Enter the value for the variable b:
    Enter the value for the variable x:
    Enter the value for the variable y: 0
    
    


  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You do know that you're actually supposed to be supplying it with numbers, right?


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

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    5

    I know but

    Quote Originally Posted by quzah View Post
    You do know that you're actually supposed to be supplying it with numbers, right?


    Quzah.
    Cygwin is not giving me the option to supply it with numbers for some reason...I am trying to figure out why.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK, this is going to sound like strange advice, but try calling fflush( stdout ); after each printf call.

  10. #10
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    The cygwin environment still produced the same results even with the fflush( stdout ) function being called.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What do you get from this command - it prints the compiler version

    Code:
    $ gcc --version
    gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
    Copyright (C) 2009 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Another thing to try is print the scanf return result, like
    Code:
        int a, r;
        printf("\nEnter the value for the variable a: ");
        fflush(stdout);
        r = scanf("%d", &a);
        printf("scanf returned %d\n",r);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  2. Issues calling a function from within a function.
    By civix in forum C++ Programming
    Replies: 12
    Last Post: 11-06-2010, 12:23 AM
  3. function issues, please help!
    By mattagrimonti in forum C Programming
    Replies: 4
    Last Post: 12-05-2008, 02:52 AM
  4. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  5. Function issues
    By tammo21 in forum C Programming
    Replies: 3
    Last Post: 08-18-2002, 08:45 AM