home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!caen!batcomputer!munnari.oz.au!bruce.cs.monash.edu.au!monu6!giaeb!s1110238
- From: s1110238@giaeb.cc.monash.edu.au (Lee Hollingworth)
- Subject: Re: HELP with scanf/getchar!
- Message-ID: <s1110238.721534396@giaeb>
- Sender: news@monu6.cc.monash.edu.au (Usenet system)
- Organization: Monash University, Melb., Australia.
- References: <1992Nov9.162547.11163@seas.smu.edu> <1992Nov9.205548.5984@bcrka451.bnr.ca>
- Date: Thu, 12 Nov 1992 02:13:16 GMT
- Lines: 80
-
- sjm@bcrki65.bnr.ca (Stuart MacMartin) writes:
-
- >In article <1992Nov9.162547.11163@seas.smu.edu> pedersen@seas.smu.edu (Ted Pedersen) writes:
- >>
- >>The following is an annoying little problem that I just can't figure
- >>out. I'm trying to get the program to prompt me for how many
- >>characters I want to enter and then once I have said how many
- >>characters I want I want the program to ask me to input those
- >>characters.
- >>
- >>What really happens is that it asks me how many characters I want. I
- >>input that and then my program sits. When I input the appropriate
- >>number of characters the program goes ahead and prints out the message
- >>asking for characters and then runs the program ok.
- >>
- >>It seems like this is running out of order. Anyone have any idea as to
- >>what might be going on?
- >>
- >>Thanks,
- >>Ted Pedersen
- >>--------------------------------------------------------------------
- >You need to flush your buffers. This is normally done with a '\n', but
- >if you don't have that, you need to explicitly call fflush:
-
- >> printf ("Enter number of characters you want : ");
- > fflush(stdout); /* <=== THIS LINE WAS MISSING */
-
- Shouldn't that be "fflush(stdin)" ?
- However if you look at the ANSI standard library it says that using fflush
- "on an input stream, the effect is undefined", therefore a more portable
- method is to define a macro...
-
- #define myflush() while (getchar() != '\n)
-
- Of course the macro has to be used *after* user input not before.
-
- However from the little bit of code left in this posting I would think
- you are trying to do unbuffered input which is not possible with standard
- C library functions. (Unless of course you wish to let the user add as many
- characters as they like but only read 'x' of them into your array).
-
- #include <stdio.h>
-
- #define myflush() while (getchar() != '\n')
-
- int main(void)
- {
- int count;
- char word[81];
- int i;
-
- printf("Enter number of characters: ");
- if (scanf("%d", &count) == 0) {
- puts("Non-integer entered");
- return (1);
- }
- myflush();
- if (count > 80) {
- printf("Array dimensions exceeded\n");
- return (1);
- }
- printf("Enter %d characters: ", count);
- for (i = 0; i < count; ++i) {
- if((word[i] = getchar()) == '\n') {
- /* print warning or something */
- break;
- }
- word[i+1] = 0; /* avoid junk in memory for newline test */
- }
- if (word[i] != '\n') {
- myflush();
- }
- word[i] = '\0';
- printf("%s\n", word);
- return (0);
- }
-
-
- Lee Hollingworth
- s1110238@giaeb.cc.monash.edu.au
-