Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting user input in Linux?

Reply
Thread Tools

Getting user input in Linux?

 
 
Zach
Guest
Posts: n/a
 
      01-31-2008
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
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2008
In article <(E-Mail Removed)>,
Zach <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      01-31-2008
Walter Roberson wrote:
> In article
> <(E-Mail Removed)>,
> Zach <(E-Mail Removed)> 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


 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      01-31-2008
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


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-31-2008
On 31 Jan, 03:17, Zach <(E-Mail Removed)> 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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-31-2008
In article <fnrta7$aqb$(E-Mail Removed)>,
Joachim Schmitz <(E-Mail Removed)> wrote:
>Walter Roberson wrote:
>> In article
>> <(E-Mail Removed)>,
>> Zach <(E-Mail Removed)> 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
 
Reply With Quote
 
Harald van Dijk
Guest
Posts: n/a
 
      01-31-2008
On Thu, 31 Jan 2008 15:21:31 +0000, Walter Roberson wrote:
> In article <fnrta7$aqb$(E-Mail Removed)>,
> Joachim Schmitz <(E-Mail Removed)> wrote:
>>Walter Roberson wrote:
>>> In article
>>> <(E-Mail Removed)>,
>>> Zach <(E-Mail Removed)> 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.
 
Reply With Quote
 
John Bode
Guest
Posts: n/a
 
      01-31-2008
On Jan 30, 10:17 pm, Zach <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting user input from user on a webpage? Adam Lee Ruby 4 06-10-2009 02:41 PM
Entering strings as user input but interpreting as Python input (sortof) Chris Carlen Python 1 09-18-2007 01:58 AM
how to restrict user from input in <input type="file"> Tarkeshwar Javascript 2 04-17-2006 11:44 AM
Getting User Input after getting Input from a file dei3cmix@uga.edu C++ 3 03-23-2006 05:01 AM
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM



Advertisments