PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Getting user input in Linux?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Getting user input in Linux?

Réponse
 
LinkBack Outils de la discussion
Vieux 31/01/2008, 03h17   #1
Zach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Getting user input in Linux?

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
  Réponse avec citation
Vieux 31/01/2008, 03h42   #2
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

In article <695fa4aa-12b5-4128-93cf-64c76594dffb@m34g2000hsb.googlegroups.com>,
Zach <netrek@gmail.com> 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
  Réponse avec citation
Vieux 31/01/2008, 07h28   #3
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

Walter Roberson wrote:
> In article
> <695fa4aa-12b5-4128-93cf-64c76594dffb@m34g2000hsb.googlegroups.com>,
> Zach <netrek@gmail.com> 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


  Réponse avec citation
Vieux 31/01/2008, 07h33   #4
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

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


  Réponse avec citation
Vieux 31/01/2008, 08h50   #5
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

On 31 Jan, 03:17, Zach <net...@gmail.com> 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
  Réponse avec citation
Vieux 31/01/2008, 15h21   #6
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

In article <fnrta7$aqb$1@online.de>,
Joachim Schmitz <jojo@schmitz-digital.de> wrote:
>Walter Roberson wrote:
>> In article
>> <695fa4aa-12b5-4128-93cf-64c76594dffb@m34g2000hsb.googlegroups.com>,
>> Zach <netrek@gmail.com> 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
  Réponse avec citation
Vieux 31/01/2008, 18h06   #7
Harald van Dijk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

On Thu, 31 Jan 2008 15:21:31 +0000, Walter Roberson wrote:
> In article <fnrta7$aqb$1@online.de>,
> Joachim Schmitz <jojo@schmitz-digital.de> wrote:
>>Walter Roberson wrote:
>>> In article
>>> <695fa4aa-12b5-4128-93cf-64c76594dffb@m34g2000hsb.googlegroups.com>,
>>> Zach <netrek@gmail.com> 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.
  Réponse avec citation
Vieux 31/01/2008, 20h14   #8
John Bode
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Getting user input in Linux?

On Jan 30, 10:17 pm, Zach <net...@gmail.com> 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h58.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14043 seconds with 16 queries