Thread: printf() not displaying the value from scanf()

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    printf() not displaying the value from scanf()

    Hi,

    I'm a newbie to both C and programing in general, so I apologize if my question seems stupid. The problem is that whenever I try to get the user to input a number with scanf(), and output with printf(), the value comes up as 0 or 0.00. Below is a sample of what I've tried:

    #include<stdio.h>

    int main()
    {
    float example[5];
    printf("please input a number.\n");
    scanf(" %.02f" , example);
    printf(" %.02f" , example);

    system("PAUSE");
    return 0;
    }

    I've tried compiling and execution on two different machines, with both Visual C++ and Dev C++. Can anyone tell me what I'm doing wrong? Thanks in advance,

    Oot

  2. #2
    wml2003
    Guest
    ur program have problem
    it should be
    scanf("%.02f",&example);

    but not scanf("%.02",example);

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Not only this, your example is an array, declare it as a simple variable, like this:
    Code:
    float example;
    And then get the data, with the follow code:
    Code:
    scanf("%f",&example);

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Or:
    Code:
    float example[5];
    printf("please input a number.\n");
    scanf("%f" , example);
    printf(" %.02f" , example[0]);
    This version uses the first element of the array only.
    To use a different element:

    scanf("%f" , &example[1]);
    printf(" %.02f" , example[1]);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems reading entered race times C
    By loopymoo26 in forum C Programming
    Replies: 12
    Last Post: 05-23-2009, 07:38 AM
  2. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 10:28 PM
  5. Quzah's printf and scanf tutorial...
    By quzah in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 03:59 AM