Re: Getting user input in Linux?



On 31 Jan, 03:17, Zach <net...@xxxxxxxxx> 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
.



Relevant Pages

  • Re: Input Verification
    ... >Additionally, despite having read my documentation for scanf(), I'm ... int verify_input ... it off beforehand) if you're getting input from fgets(). ... terminates the valid input immediately. ...
    (comp.lang.c)
  • Re: Need some help with I/O
    ... >> could someone give me a suggestion? ... >> int main ... > array 'store'. ... My textboox has less than a paragraph on the fgets() function ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Help the helpless.. code read
    ... > int sensors; ... You wrote exactly 40 characters to the file. ... Telling fgets to read 80 characters is guaranteed ... As several of us have already stated, neither roms nor *roms is an ...
    (comp.lang.c)
  • Re: scanf
    ... int main{ ... call to fgets will place "sal" into input. ... store "ome" into input. ... sscanf will again return 0. ...
    (comp.lang.c)
  • Re: gets() fgets() with subdivide.c tilepack.c squarect.c
    ... extern int atoi(); ... Note the correct use of 'fgets' above. ... a few of the lines of code about which the compiler was complaining. ... That's my best guess as to what these error messages mean; ...
    (comp.lang.c)