1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

scanf calling

Discussion in 'C' started by fsakalos, May 15, 2007.

  1. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    Hi all, I have simple question. Do you know how to call character written during scanf back?
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    I'm not sure I understand your question. Scanf doesn't write, it reads. Once you have read a character with scanf, you cannot put it back into the stream in any portable way. If you are using buffered input and if you understand how your particular implementation works, you can write your own functions to peep at the next character before taking it, or to put the last character taken back into the buffer.

    Generally speaking, this is how C input works: The operating system collects and manipulates keyboard input and places the characters into an OS buffer. This is not a buffer manipulable by the programmer.

    When the programmer calls an input function, the language makes a blocking call for input using a low-level read. This call does not return until an end of line or end of file is found. When that condition is detected, the collected characters are put into the C input stream. If the stream is not buffered, some data may be lost. It depends on how the programmer uses the various input functions.

    Functions that are not provided by the language may be written by the programmer. This act requires a knowledge of the platform and OS in use. For instance, one may do just about anything by writing functions that use the Win Console API. Similarly, but not the same, for Unix variants.
     
  3. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    OK, I'll change my question. I have a variable named "number". Until some point it doesn't have any value but I want to assign a value for it. User will set that value. This should be program for power of 2 but not only. I want to have a chart like he writes 5 so it will be Pof2 for 5, 6, 7, etc. Problem is, that it always starts from 0 and not from user's value.
     
  4. fsakalos

    fsakalos New Member

    Joined:
    May 14, 2007
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Slovakia
    Please answer something
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Man scanf and read the documentation carefully. You also need to consider how to provide information with your question. You don't even mention the language. This is for C. C++ has overloaded pow functions.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        double base = 2.0;
        double exp;
    
        while (1)
        {
            printf ("Enter the exponent: ");
            if (scanf ("%lf", &exp) == 1)
            {
                printf ("2 raised to the power of %f is %f\n", exp, pow (base, exp));
            }
            else 
            {
                printf ("Invalid input.  ");
                break;
            }
        }
        return 0;
    }
    
     

Share This Page