Troubles using C's scanf Function
Author |
Message |
Amos Freu #1 / 12
|
 Troubles using C's scanf Function
Well, i'm a C beginner. i've just switched from Pascal. to the point: after writing the following lines: (where v is a one-dimension array, size 7) printf ("enter the numbers "); for (count=0;count<=6;count++) scanf("%d",v[count] everything runs correctly, but the nubers i type, won't stay in v[count]. it remains zero. why ? thanx in advnace Mr. Amos Freund
"200Kph on a big bore UJM. THE fun!"
|
Sat, 08 May 1999 03:00:00 GMT |
|
 |
Vladimir Beker /bwz #2 / 12
|
 Troubles using C's scanf Function
Quote:
> after writing the following lines: > (where v is a one-dimension array, size 7) > printf ("enter the numbers "); > for (count=0;count<=6;count++) > scanf("%d",v[count] > everything runs correctly, but the nubers i type, won't stay in > v[count]. it remains zero. why ?
You must write: for ( count = 0 ; count < 7 ; count++ ) // the same that you wrote just more C-like scanf ("%d", &v[count]); // use address of variable C-compiler cannot detect such error, because scanf defined as: int scanf (const char * format, ... ); Good luck with C Vladimir
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Scott Drage #3 / 12
|
 Troubles using C's scanf Function
What else do you have going on? How did you initialize the array? This looks correct to me.(well except for no paras-which I assume was just for posting) Scott D. Quote:
> Well, i'm a C beginner. > i've just switched from pascal. > to the point: > after writing the following lines: > (where v is a one-dimension array, size 7) > printf ("enter the numbers "); > for (count=0;count<=6;count++) > scanf("%d",v[count] > everything runs correctly, but the nubers i type, won't stay in > v[count]. it remains zero. why ? > thanx in advnace > Mr. Amos Freund
> "200Kph on a big bore UJM. THE fun!"
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Scott Drage #4 / 12
|
 Troubles using C's scanf Function
Quote:
> > after writing the following lines: > > (where v is a one-dimension array, size 7) > > printf ("enter the numbers "); > > for (count=0;count<=6;count++) > > scanf("%d",v[count] > > everything runs correctly, but the nubers i type, won't stay in > > v[count]. it remains zero. why ? > You must write: > for ( count = 0 ; count < 7 ; count++ ) // the same that you wrote just more C-like > scanf ("%d", &v[count]); // use address of variable > C-compiler cannot detect such error, because scanf defined as: int scanf (const char * format, ... ); > Good luck with C > Vladimir
Oops, my mistake, Vladimir is correct.
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Hans Steffa #5 / 12
|
 Troubles using C's scanf Function
Quote:
>Well, i'm a C beginner. >i've just switched from pascal. >to the point: >after writing the following lines: >(where v is a one-dimension array, size 7) >printf ("enter the numbers "); >for (count=0;count<=6;count++) > scanf("%d",v[count] >everything runs correctly, but the nubers i type, won't stay in >v[count]. it remains zero. why ?
scanf() expects in this case a pointer to an int but not an int itsself. Try scanf("%d", &v[count] ); h.f.s. cc,f'up -- Hans Friedrich Steffani Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz-Zwickau
http://www.tu-chemnitz.de/~hfst/
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
P.Benne #6 / 12
|
 Troubles using C's scanf Function
Quote:
>Well, i'm a C beginner. >i've just switched from pascal. >to the point: >after writing the following lines: >(where v is a one-dimension array, size 7) >printf ("enter the numbers "); >for (count=0;count<=6;count++) > scanf("%d",v[count] >everything runs correctly, but the nubers i type, won't stay in >v[count]. it remains zero. why ?
You need to give scanf() the _address_ of the variable you want it to write to, so the scanf() line should be: scanf("%d", &v[count]); ^^ Incidently, scanf() is often unpleasant for direct user input - unexpected input (letters when it expects a number, for example) can cause problems. It may be better to get a whole line with fgets(), then you can examine it at your leisure, scan it with sscanf(), pick it apart with strtok(), etc. Generally much easier to recover from errors this way... Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k) GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html or: http://vancouver-webpages.com/peter/index.html
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Martin Ambu #7 / 12
|
 Troubles using C's scanf Function
19:10:08 GMT): :Well, i'm a C beginner. :i've just switched from pascal. :to the point: :after writing the following lines: :(where v is a one-dimension array, size 7) :printf ("enter the numbers "); It is a good idea to follow the above with fflush(stdout); to insure that the output appears before you attempt to read below :for (count=0;count<=6;count++) : scanf("%d",v[count] The function scanf() needs to know where to put the read data, not its value. v[count] as a parameter to scanf() is just the value that is already in v[count]. If you had been running on a more sophisticated machine, you would have encountered an addressing error on running this; with a better compiler, you would have been warned that v[count] was not a pointer. To provide the address, you need to use the `&' operator: scanf("%d",&v[count]); Having said all that, the scanf() function is not the preferred way to do input, since it will not respond well to ill-conditioned input. Get the FAQ by anonymous FTP from rtfm.mit.edu, where alternatives are discussed. You probably need to learn more C before many things in the FAQ make sense, but it should still be an early stop (before comp.lang.c) when you have more questions. :everything runs correctly, but the nubers i type, won't stay in :v[count]. it remains zero. why ? :thanx in advnace :Mr. Amos Freund
:"200Kph on a big bore UJM. THE fun!"
Honors Bridge Club, 115 E 57th, New York
* all newsgroups follow-ups are also emailed */
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Martin Ambu #8 / 12
|
 Troubles using C's scanf Function
(comp.lang.c, Tue, 26 Nov 1996 08:14:27 -0600):
:>
:> > :> > after writing the following lines: :> > (where v is a one-dimension array, size 7) :> > printf ("enter the numbers "); :> > for (count=0;count<=6;count++) :> > scanf("%d",v[count] :> > :> > everything runs correctly, but the nubers i type, won't stay in :> > v[count]. it remains zero. why ? :> :> You must write: :> for ( count = 0 ; count < 7 ; count++ ) // the same that you wrote just more C-like :> scanf ("%d", &v[count]); // use address of variable :> :> C-compiler cannot detect such error, because scanf defined as: int scanf (const char * format, ... ); :> Good luck with C :> Vladimir :Oops, my mistake, Vladimir is correct. SIGH. Vladimir is right only in inserting the `&' operator. He is wrong about changing the for-loop, about needing to insert `// ' syntax errors, and about what the capabilities of a C compiler are. His net score is clearly minus.
Honors Bridge Club, 115 E 57th, New York
* all newsgroups follow-ups are also emailed */
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Martin Ambu #9 / 12
|
 Troubles using C's scanf Function
(comp.lang.c, Tue, 26 Nov 1996 15:17:01 +0200): : :You must write: : for ( count = 0 ; count < 7 ; count++ ) // the same that you wrote just more C-like : scanf ("%d", &v[count]); // use address of variable There is no reason to tell him he "must" write for(count = 0; count < 7; count++) instead of for(count = 0; count <= 6; count++) It is in no way more "C-like". What is NOT "C-like" is telling him that he "must" use syntax errors. `// ' is not a comment-introduction. It is an error. An ANSI-compliant C compiler will find it so. :C-compiler cannot detect such error, because scanf defined as: int scanf (const char * format, ... ); :Good luck with C Get a new C compiler. Mine has no trouble detecting such an error. Or the ones you say we "must" introduce. :Vladimir
Honors Bridge Club, 115 E 57th, New York
* all newsgroups follow-ups are also emailed */
|
Sat, 15 May 1999 03:00:00 GMT |
|
 |
Gabor Egres #10 / 12
|
 Troubles using C's scanf Function
: >Well, i'm a C beginner. : >i've just switched from pascal. : >to the point: : > : >after writing the following lines: : >(where v is a one-dimension array, size 7) : >printf ("enter the numbers "); : >for (count=0;count<=6;count++) : > scanf("%d",v[count] : > : >everything runs correctly, but the nubers i type, won't stay in : >v[count]. it remains zero. why ? : You need to give scanf() the _address_ of the variable you want it to write to, : so the scanf() line should be: : scanf("%d", &v[count]); : ^^ : Incidently, scanf() is often unpleasant for direct user input - unexpected : input (letters when it expects a number, for example) can cause problems. It : may be better to get a whole line with fgets(), then you can examine it at your : leisure, scan it with sscanf(), pick it apart with strtok(), etc. Generally : much easier to recover from errors this way... I agree with you a 100%. I just wonder why nobody suggested the simplest thing if you insis on scanf and that is scanf("%d",v + count); Never mind of course that 'v' as a variable name is plain stupid. What the heck does it stand for? velocity? vector? venom? victory? --
If it doesn't work, you're doing it wrong, If it does, you just got lucky.
|
Sun, 16 May 1999 03:00:00 GMT |
|
 |
Hans Steffa #11 / 12
|
 Troubles using C's scanf Function
Quote:
>I agree with you a 100%. I just wonder why nobody suggested the simplest >thing if you insis on scanf and that is > scanf("%d",v + count); >Never mind of course that 'v' as a variable name is plain stupid. What >the heck does it stand for? velocity? vector? venom? victory?
I did not post this is version for pedagogical reasons. If one writes scanf("%d",v[count]); it is easy to say: remember, you would never use scanf("%d", f ); but alsway scanf("%d", &f ); assuming f a float variable so why don't you use scanf("%d",&v[count]); ? If he understand that scanf() always needs a pointer to something we can explain in a several lesson that &v[count] is the same as &(*(v+count)) which is the same as v+count. Now no teacher is needed to come to the proposed version. However I normaly prefer &v[count] but that is personal taste. h.f.s. -- Hans Friedrich Steffani Institut fuer Elektrische Maschinen und Antriebe, TU Chemnitz-Zwickau
http://www.tu-chemnitz.de/~hfst/
|
Mon, 17 May 1999 03:00:00 GMT |
|
 |
Christopher Hu #12 / 12
|
 Troubles using C's scanf Function
Quote:
>Well, i'm a C beginner. >i've just switched from pascal. >to the point: >after writing the following lines: >(where v is a one-dimension array, size 7) >printf ("enter the numbers "); >for (count=0;count<=6;count++) > scanf("%d",v[count] >everything runs correctly, but the nubers i type, won't stay in >v[count]. it remains zero. why ?
I too am new at C programming, but am learning it as part of course at college. Our class intially ran into this problem as well. The solution is to change: scanf("%d",v[count] to: scanf("%d",&v[count]) In otherwords the ampersand, i.e. the & symbol must precede your variable in the scanf statement. I hope this helps.
|
Mon, 24 May 1999 03:00:00 GMT |
|
|
|