Getting user input in Linux?

Discussion in 'C Programming' started by Zach, Jan 31, 2008.

  1. Zach

    Zach Guest

    Hi,

    I run Linux 2.6.18 and am seeking a way in C to get user input. I want
    them to have the chance to enter a value and have it assigned to a
    variable.

    I currently have:

    int* s1;

    printf("Enter first integer: %d\n",s1);
    scanf(&s1);

    When I run this it automatically enters 0. It never pauses and thus
    does not allow me to type in a value.

    Zach
     
    Zach, Jan 31, 2008
    #1
    1. Advertising

  2. In article <>,
    Zach <> wrote:

    >I run Linux 2.6.18 and am seeking a way in C to get user input. I want
    >them to have the chance to enter a value and have it assigned to a
    >variable.


    >I currently have:


    >int* s1;


    >printf("Enter first integer: %d\n",s1);
    >scanf(&s1);


    >When I run this it automatically enters 0. It never pauses and thus
    >does not allow me to type in a value.


    You start out by creating a pointer to an integer. You do not
    set that pointer to any particular value, but you then try to
    print out whatever random value it has, and you try to do so
    using a format (%d) that tells printf that you are printing an
    integer rather than the pointer-to-an-integer that you are passing.

    After that you do nothing special before you try to read some
    data. However, if you want your output to appear before the
    read starts happening, you have to call fflush(stdout)

    You then try to read some text data and have it parsed as
    a number of some kind. However, in the place where you are
    supposed to pass a format telling what kind of number to read,
    you are passing a pointer to s1, so you are passing a pointer
    to the (uninitialized) pointer to an integer . But you got *un*lucky
    and scanf() did NOT just bomb and tell you something was wrong.
    So s1 did not get read to, and whatever your program did after
    that point continued to use whatever random value s1 had.


    Your attempt has so many mistakes that it seems unlikely that you
    have examined even a simple tutorial on reading data with C.
    Rather than just giving you the code, I will give you advice:

    Go and read a C tutorial

    If I just gave you the code, you would be back with other things
    you did not understand because you had not read up on fundamental
    parts of C.
    --
    "I was very young in those days, but I was also rather dim."
    -- Christopher Priest
     
    Walter Roberson, Jan 31, 2008
    #2
    1. Advertising

  3. Walter Roberson wrote:
    > In article
    > <>,
    > Zach <> wrote:
    >
    >> I run Linux 2.6.18 and am seeking a way in C to get user input. I
    >> want them to have the chance to enter a value and have it assigned
    >> to a variable.

    >
    >> I currently have:

    >
    >> int* s1;

    >
    >> printf("Enter first integer: %d\n",s1);
    >> scanf(&s1);

    <snip>
    > After that you do nothing special before you try to read some
    > data. However, if you want your output to appear before the
    > read starts happening, you have to call fflush(stdout)

    the '\n' in the printf should have done than.

    Bye, Jojo
     
    Joachim Schmitz, Jan 31, 2008
    #3
  4. Zach wrote:
    > Hi,
    >
    > I run Linux 2.6.18 and am seeking a way in C to get user input. I want
    > them to have the chance to enter a value and have it assigned to a
    > variable.
    >
    > I currently have:
    >
    > int* s1;

    int s1

    > printf("Enter first integer: %d\n",s1);
    > scanf(&s1);

    scanf("%d", &s1);

    > When I run this it automatically enters 0. It never pauses and thus
    > does not allow me to type in a value.
    >
    > Zach


    Still far from being perfect and safe, but might get you started

    Bye, Jojo
     
    Joachim Schmitz, Jan 31, 2008
    #4
  5. On 31 Jan, 03:17, Zach <> wrote:

    > I run Linux 2.6.18 and am seeking a way in C to get user input. I want
    > them to have the chance to enter a value and have it assigned to a
    > variable.
    >
    > I currently have:
    >
    > int* s1;
    >
    > printf("Enter first integer: %d\n",s1);
    > scanf(&s1);
    >
    > When I run this it automatically enters 0. It never pauses and thus
    > does not allow me to type in a value.


    note scanf() can be tricky. It might be better to use fgets()
    followed by atoi() or (better) strtol(). Read the documentation for
    them.

    Oh, and try and get a copy of K&R


    --
    Nick Keighley

    The fscanf equivalent of fgets is so simple
    that it can be used inline whenever needed:-
    char s[NN + 1] = "", c;
    int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
    if (rc == 1) fscanf("%*[^\n]%*c);
    if (rc == 0) getc(fp);

    Dan Pop
     
    Nick Keighley, Jan 31, 2008
    #5
  6. In article <fnrta7$aqb$>,
    Joachim Schmitz <> wrote:
    >Walter Roberson wrote:
    >> In article
    >> <>,
    >> Zach <> wrote:


    >>> printf("Enter first integer: %d\n",s1);
    >>> scanf(&s1);


    >> After that you do nothing special before you try to read some
    >> data. However, if you want your output to appear before the
    >> read starts happening, you have to call fflush(stdout)


    >the '\n' in the printf should have done than.


    The '\n' in the printf would only have that effect if the
    output was line-buffered or unbuffered instead of file-buffered.
    The default for streams is file-buffering, except when the
    system can detect that the stream is connected to a "terminal".
    What a "terminal" is and whether it can be reliably detected is
    not defined by the standard. It is safer to assume that detection
    is unreliable and to fflush(stdout) manually.

    Historically, there has been interactive usage that was not
    reliably detected, such as in the earlier generations of operations
    of unix pseudo-terminals (ptys).
    --
    "There are some ideas so wrong that only a very intelligent person
    could believe in them." -- George Orwell
     
    Walter Roberson, Jan 31, 2008
    #6
  7. On Thu, 31 Jan 2008 15:21:31 +0000, Walter Roberson wrote:
    > In article <fnrta7$aqb$>,
    > Joachim Schmitz <> wrote:
    >>Walter Roberson wrote:
    >>> In article
    >>> <>,
    >>> Zach <> wrote:

    >
    >>>> printf("Enter first integer: %d\n",s1); scanf(&s1);

    >
    >>> After that you do nothing special before you try to read some data.
    >>> However, if you want your output to appear before the read starts
    >>> happening, you have to call fflush(stdout)

    >
    >>the '\n' in the printf should have done than.

    >
    > The '\n' in the printf would only have that effect if the output was
    > line-buffered or unbuffered instead of file-buffered. The default for
    > streams is file-buffering, except when the system can detect that the
    > stream is connected to a "terminal". What a "terminal" is and whether it
    > can be reliably detected is not defined by the standard.


    It's the other way around. Quoting 7.19.3p7:
    "the standard input and standard output streams are fully buffered if and
    only if the stream can be determined not to refer to an interactive
    device."

    If the implementation can't reliably detect interactive devices, it is
    not allowed to make the standard input and output streams fully buffered
    by default.

    > It is safer to
    > assume that detection is unreliable and to fflush(stdout) manually.
    >
    > Historically, there has been interactive usage that was not reliably
    > detected, such as in the earlier generations of operations of unix
    > pseudo-terminals (ptys).


    It *is* safer, I'll admit that, but for different reasons: if you
    redirect stdout to a file, and another process reads this file and prints
    data to the screen as soon as it is written, that doesn't change the fact
    the first process is writing to a provably non-interactive file, and
    fully buffered mode is permitted.
     
    Harald van Dijk, Jan 31, 2008
    #7
  8. Zach

    John Bode Guest

    On Jan 30, 10:17 pm, Zach <> wrote:
    > Hi,
    >
    > I run Linux 2.6.18 and am seeking a way in C to get user input. I want
    > them to have the chance to enter a value and have it assigned to a
    > variable.
    >
    > I currently have:
    >
    > int* s1;
    >
    > printf("Enter first integer: %d\n",s1);
    > scanf(&s1);
    >
    > When I run this it automatically enters 0. It never pauses and thus
    > does not allow me to type in a value.
    >
    > Zach


    2 questions:

    1. Are you *sure* this is the code you wrote?

    2. Did you #include <stdio.h>?

    I ask because the compiler should yak on the scanf() line as written.
    scanf() expects the first parameter to be of type const char *, not
    int**.

    Here's a minimal example of how that code should be written:

    #include <stdio.h>

    int main(void)
    {
    int s1;

    printf("Enter the first integer: ");
    fflush(stdout);

    if (scanf("%d", &s1) < 1)
    {
    printf("Input value was not an integer\n");
    }
    else
    {
    printf("You entered %d\n", s1);
    }
    return EXIT_SUCCESS;
    }

    scanf() isn't the best tool for interactive user input; it doesn't
    handle errors all that well, and can leave garbage in the input stream
    that can cause future scanf() calls to fail. I personally prefer
    using fgets() to read everything in as a string, and then covert using
    tools like strtol() or strtod(). It gives you more flexibility in
    dealing with bad input and reduces the likelihood of buffer overruns.
     
    John Bode, Jan 31, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    680
    Walter Roberson
    Sep 9, 2005
  2. Replies:
    3
    Views:
    403
  3. Chris Carlen
    Replies:
    1
    Views:
    631
    Gabriel Genellina
    Sep 18, 2007
  4. mpnordland
    Replies:
    24
    Views:
    913
  5. Adam Lee
    Replies:
    4
    Views:
    136
    Adam Lee
    Jun 10, 2009
Loading...

Share This Page