For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2008 > Getting user input in Linux?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Getting user input in Linux?
Zach

2008-01-30, 10:24 pm

Hi,

I run Linux 2.6.18 and am sing 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 allows
me to type in a value.

Zach
Logan Shaw

2008-01-30, 10:24 pm

Zach wrote:
> I run Linux 2.6.18 and am sing 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);


You have allocated a pointer to some hypothetical storage that
would contain an integer, but you've never allocated any such
storage or assigned any value to the pointer. So there is no
correct place for your pointer to point to, and even if there
were, it is not pointing there; it is pointing to some random
memory location.

Also, your call to printf() needs to have an integer value
passed to it, not a pointer (at least when you used "%d").

Finally, scanf() requires you to pass it a format which describes
what it should look for. It's very similar to printf() in that
respect.

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


It's probably best to start by fixing the above errors rather
than spending time thinking about why it's doing what it's doing.

- Logan
Ben Bacarisse

2008-01-31, 7:27 pm

[Please don't top post and snip content and sigs where possible.]

Manik <manik.mayur@gmail.com> writes:
<snip>[color=darkred]
> How about,
>
> int s1;
> printf("Enter first integer:");
> scanf(&s1);


Won't compile (if you are lucky). Better:

int s1;
printf("Enter first integer: ");
fflush(stdout);
scanf("%d", &s1); /* scanf needs a format */

--
Ben.
Manik

2008-01-31, 7:27 pm

How about,

int s1;
printf("Enter first integer:");
scanf(&s1);

On Jan 31, 8:56 am, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> Zach wrote:
>
>
>
>
> You have allocated a pointer to some hypothetical storage that
> would contain an integer, but you've never allocated any such
> storage or assigned any value to the pointer. So there is no
> correct place for your pointer to point to, and even if there
> were, it is not pointing there; it is pointing to some random
> memory location.
>
> Also, your call to printf() needs to have an integer value
> passed to it, not a pointer (at least when you used "%d").
>
> Finally, scanf() requires you to pass it a format which describes
> what it should look for. It's very similar to printf() in that
> respect.
>
>
> It's probably best to start by fixing the above errors rather
> than spending time thinking about why it's doing what it's doing.
>
> - Logan


Zach

2008-01-31, 7:27 pm

On Jan 31, 8:32 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>
> int s1;
> printf("Enter first integer: ");
> fflush(stdout);
> scanf("%d", &s1); /* scanf needs a format */


Hi Ben,

I need s1 to be a pointer because in my program 2 pointers are passed
to my swap function and swapped.
Any idea how to do this with s1 being a pointer of type int?

Zach
Zach

2008-01-31, 7:27 pm

On Jan 30, 10:56 pm, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
>
> It's probably best to start by fixing the above errors rather
> than spending time thinking about why it's doing what it's doing.


Thanks for the feedback Logan.

Zach
Barry Margolin

2008-01-31, 7:27 pm

In article
<b816695f-02b8-4c31-b9fe-6dcee6b07fc1@i29g2000prf.googlegroups.com>,
Zach <netrek@gmail.com> wrote:

> On Jan 31, 8:32 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>
> Hi Ben,
>
> I need s1 to be a pointer because in my program 2 pointers are passed
> to my swap function and swapped.
> Any idea how to do this with s1 being a pointer of type int?
>
> Zach


Why don't you pass &s1 to the swap function?

In your original code s1 was a local variable in your function. In the
real code will it be a parameter received by your function, so that the
storage is allocated by the caller? In that case, you should write:

scanf("%d", s1);

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com