scanf calling

fsakalos's Avatar, Join Date: May 2007
Go4Expert Member
Hi all, I have simple question. Do you know how to call character written during scanf back?
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
fsakalos's Avatar, Join Date: May 2007
Go4Expert Member
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.
fsakalos's Avatar, Join Date: May 2007
Go4Expert Member
Please answer something
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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;
}
Quote:
Originally Posted by Output
Enter the exponent: 4
2 raised to the power of 4.000000 is 16.000000
Enter the exponent: 16
2 raised to the power of 16.000000 is 65536.000000
Enter the exponent: