|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
![]() |
| Outils de la discussion | |
|
|