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
|