Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > I need help please!

Reply
Thread Tools

I need help please!

 
 
Jim
Guest
Posts: n/a
 
      03-07-2008
This has got to be really simple but I can't figure it out... I got
swamped with work and am doing this project for a class that I'm
taking and I can't figure out why the output is like this.

I'm taking a Functional Programming in C and I have to write a
shuffling program. The problem is that when I ask the user if they
want to shuffle, they type y or n to answer. It then goes into a
switch statement and executes code. It exits if they type no and is
suppose to keep looping until they say no.

So... Here is the output I'm getting:

-------------
The array holds 20 elements which consists of:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20 }

Would you like to shuffle the array? (y=yes, n=no): y
You have said you would like to shuffle.
Shuffling...

Would you like to shuffle the array? (y=yes, n=no): Would you like to
shuffle the array? (y=yes, n=no):
----------------

Notice that the question is getting asked twice instead of just once
(after it loops). I added a default: line to my switch statement and
it seems to be reading something else in the userval variable but I
can't figure out what I'm doing wrong. Please help... This is due in
4 hours. -- Before anyone flames me, I didn't purposely
procrastinate on this, I work full time, have a family and didn't get
a chance to do this earlier.

Here is my code:
-----------------
#include <math.h>
21 #include <stdio.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <math.h>
25 #define SIZE 20
26
27 // function prototypes
28
29
30 // main program
31 int main(void) {
32
33 // declare variables
34 int
35 array1[SIZE]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20 },
36 cnt = 0;
37
38 bool cont = true;
39 char userval;
40
41 // while(cont) {
42 // Display the contents of the array.
43 printf("The array holds %d elements which consists of:
\n { ", SIZE);
44
45 for(cnt = 0; cnt < SIZE; cnt++) {
46 (cnt != SIZE - 1) ? printf("%d, ", array1[cnt]) :
printf("%d ", array1[cnt]);
47 }
48
49 printf("}\n\n");
50
51 while(cont) {
52 // ask if the user woule like to shuffle the contents
of the array
53 printf("Would you like to shuffle the array? (y=yes,
n=no): ");
54 scanf("%c", &userval);
55
56 switch(userval) {
57 case 'y':
58 case 'Y':
59 printf("You have said you would like to
shuffle.\nShuffling...\n\n");
60 break;
61
62 case 'n':
63 case 'N':
64 printf("No shuffling will take place.
\nExiting...\n");
65 cont = false;
66 break;
67 }
68 }
69 }
-----------------
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-07-2008
Jim wrote:

> This has got to be really simple but I can't figure it out... I got
> swamped with work and am doing this project for a class that I'm
> taking and I can't figure out why the output is like this.
>
> I'm taking a Functional Programming in C and I have to write a
> shuffling program. The problem is that when I ask the user if they
> want to shuffle, they type y or n to answer. It then goes into a
> switch statement and executes code. It exits if they type no and is
> suppose to keep looping until they say no.
>
> So... Here is the output I'm getting:
>
> -------------
> The array holds 20 elements which consists of:
> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
> 20 }
>
> Would you like to shuffle the array? (y=yes, n=no): y
> You have said you would like to shuffle.
> Shuffling...
>
> Would you like to shuffle the array? (y=yes, n=no): Would you like to
> shuffle the array? (y=yes, n=no):
> ----------------
>
> Notice that the question is getting asked twice instead of just once
> (after it loops). I added a default: line to my switch statement and
> it seems to be reading something else in the userval variable but I
> can't figure out what I'm doing wrong. Please help... This is due in
> 4 hours. -- Before anyone flames me, I didn't purposely
> procrastinate on this, I work full time, have a family and didn't get
> a chance to do this earlier.
>
> Here is my code:
> -----------------
> #include <math.h>
> 21 #include <stdio.h>
> 22 #include <stdbool.h>
> 23 #include <stdlib.h>
> 24 #include <math.h>
> 25 #define SIZE 20
> 26
> 27 // function prototypes
> 28
> 29
> 30 // main program
> 31 int main(void) {
> 32
> 33 // declare variables
> 34 int
> 35 array1[SIZE]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
> 13, 14, 15, 16, 17, 18, 19, 20 },
> 36 cnt = 0;
> 37
> 38 bool cont = true;
> 39 char userval;
> 40
> 41 // while(cont) {
> 42 // Display the contents of the array.
> 43 printf("The array holds %d elements which consists of:
> \n { ", SIZE);
> 44
> 45 for(cnt = 0; cnt < SIZE; cnt++) {
> 46 (cnt != SIZE - 1) ? printf("%d, ", array1[cnt]) :
> printf("%d ", array1[cnt]);
> 47 }
> 48
> 49 printf("}\n\n");
> 50
> 51 while(cont) {
> 52 // ask if the user woule like to shuffle the contents
> of the array
> 53 printf("Would you like to shuffle the array? (y=yes,
> n=no): ");
> 54 scanf("%c", &userval);
> 55
> 56 switch(userval) {
> 57 case 'y':
> 58 case 'Y':
> 59 printf("You have said you would like to
> shuffle.\nShuffling...\n\n");
> 60 break;
> 61
> 62 case 'n':
> 63 case 'N':
> 64 printf("No shuffling will take place.
> \nExiting...\n");
> 65 cont = false;
> 66 break;
> 67 }
> 68 }
> 69 }
> -----------------


This is a problem with your use of scanf. The second time around it
reads the '\n' character (generated by pressing ENTER) left in the
input buffer after reading in the 'y' or 'n' character preceding it.
You need to flush out the excessive characters before calling scanf.
You can do that with a simple loop with fgetc, reading until you
encounter a '\n' character, like say:

void flush_input(void) {
int ch;
while ((ch = fgetc(stdin)) != '\n')
;
return;
}

You should call this after each scanf call above.

But a more robust solution is to drop scanf and use fgets to read in a
whole line and examine that line yourself.

 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      03-07-2008
Jim wrote:
) This has got to be really simple but I can't figure it out... I got
) swamped with work and am doing this project for a class that I'm
) taking and I can't figure out why the output is like this.
)
) I'm taking a Functional Programming in C and I have to write a
) shuffling program. The problem is that when I ask the user if they
) want to shuffle, they type y or n to answer. It then goes into a
) switch statement and executes code. It exits if they type no and is
) suppose to keep looping until they say no.
)
) So... Here is the output I'm getting:
)
) -------------
) The array holds 20 elements which consists of:
) { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
) 20 }
)
) Would you like to shuffle the array? (y=yes, n=no): y
) You have said you would like to shuffle.
) Shuffling...
)
) Would you like to shuffle the array? (y=yes, n=no): Would you like to
) shuffle the array? (y=yes, n=no):
) ----------------
)
) Notice that the question is getting asked twice instead of just once
) (after it loops). I added a default: line to my switch statement and
) it seems to be reading something else in the userval variable but I
) can't figure out what I'm doing wrong.

My guess is that the 'something else' it is reading is a newline.
Before reading the response, code a loop that discards input characters
until you find a newline.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Jim
Guest
Posts: n/a
 
      03-07-2008
On Mar 7, 12:17 pm, santosh <(E-Mail Removed)> wrote:
> Jim wrote:
> > This has got to be really simple but I can't figure it out... I got
> > swamped with work and am doing this project for a class that I'm
> > taking and I can't figure out why the output is like this.

>
> > I'm taking a Functional Programming in C and I have to write a
> > shuffling program. The problem is that when I ask the user if they
> > want to shuffle, they type y or n to answer. It then goes into a
> > switch statement and executes code. It exits if they type no and is
> > suppose to keep looping until they say no.

>
> > So... Here is the output I'm getting:

>
> > -------------
> > The array holds 20 elements which consists of:
> > { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
> > 20 }

>
> > Would you like to shuffle the array? (y=yes, n=no): y
> > You have said you would like to shuffle.
> > Shuffling...

>
> > Would you like to shuffle the array? (y=yes, n=no): Would you like to
> > shuffle the array? (y=yes, n=no):
> > ----------------

>
> > Notice that the question is getting asked twice instead of just once
> > (after it loops). I added a default: line to my switch statement and
> > it seems to be reading something else in the userval variable but I
> > can't figure out what I'm doing wrong. Please help... This is due in
> > 4 hours. -- Before anyone flames me, I didn't purposely
> > procrastinate on this, I work full time, have a family and didn't get
> > a chance to do this earlier.

>
> > Here is my code:
> > -----------------
> > #include <math.h>
> > 21 #include <stdio.h>
> > 22 #include <stdbool.h>
> > 23 #include <stdlib.h>
> > 24 #include <math.h>
> > 25 #define SIZE 20
> > 26
> > 27 // function prototypes
> > 28
> > 29
> > 30 // main program
> > 31 int main(void) {
> > 32
> > 33 // declare variables
> > 34 int
> > 35 array1[SIZE]= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
> > 13, 14, 15, 16, 17, 18, 19, 20 },
> > 36 cnt = 0;
> > 37
> > 38 bool cont = true;
> > 39 char userval;
> > 40
> > 41 // while(cont) {
> > 42 // Display the contents of the array.
> > 43 printf("The array holds %d elements which consists of:
> > \n { ", SIZE);
> > 44
> > 45 for(cnt = 0; cnt < SIZE; cnt++) {
> > 46 (cnt != SIZE - 1) ? printf("%d, ", array1[cnt]) :
> > printf("%d ", array1[cnt]);
> > 47 }
> > 48
> > 49 printf("}\n\n");
> > 50
> > 51 while(cont) {
> > 52 // ask if the user woule like to shuffle the contents
> > of the array
> > 53 printf("Would you like to shuffle the array? (y=yes,
> > n=no): ");
> > 54 scanf("%c", &userval);
> > 55
> > 56 switch(userval) {
> > 57 case 'y':
> > 58 case 'Y':
> > 59 printf("You have said you would like to
> > shuffle.\nShuffling...\n\n");
> > 60 break;
> > 61
> > 62 case 'n':
> > 63 case 'N':
> > 64 printf("No shuffling will take place.
> > \nExiting...\n");
> > 65 cont = false;
> > 66 break;
> > 67 }
> > 68 }
> > 69 }
> > -----------------

>
> This is a problem with your use of scanf. The second time around it
> reads the '\n' character (generated by pressing ENTER) left in the
> input buffer after reading in the 'y' or 'n' character preceding it.
> You need to flush out the excessive characters before calling scanf.
> You can do that with a simple loop with fgetc, reading until you
> encounter a '\n' character, like say:
>
> void flush_input(void) {
> int ch;
> while ((ch = fgetc(stdin)) != '\n')
> ;
> return;
>
> }
>
> You should call this after each scanf call above.
>
> But a more robust solution is to drop scanf and use fgets to read in a
> whole line and examine that line yourself.


thank you so much! I thought that it was the input buffer but I guess
I was handling it wrong. I thought that fflush(stdin) would clear the
buffer.

It works like a charm.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-07-2008
Jim wrote:

<snip>

> thank you so much! I thought that it was the input buffer but I guess
> I was handling it wrong. I thought that fflush(stdin) would clear the
> buffer.


Actually fflush is defined only for output streams and calling fflush on
stdin is totally non-portable, i.e., it might seem to work for your
current compiler, and it might lead to a system crash on the very next
one you try the code on.

> It works like a charm.


But it will block if stdin is already at end-of-file and if there are
no '\n' in the input buffer.

For the long term using something like fgets followed by custom parsing
would be much more robust.

 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      03-08-2008

"santosh" <(E-Mail Removed)> wrote in message
news:fqrush$r7l$(E-Mail Removed)...

[Simple stdin input]
> But it will block if stdin is already at end-of-file and if there are
> no '\n' in the input buffer.
>
> For the long term using something like fgets followed by custom parsing
> would be much more robust.
>


I was playing with the following code for reading a character at a time, to
do the same sort of thing as the OP.

But fgetc() doesn't return until end-of-line (Enter) is pressed. Is this
normal behaviour?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
while (1)
{ switch (fgetc(stdin))
{ case 'Y': case 'y':
puts("es");
break;
case 'N': case 'n':
puts("o");
break;
case 'Q': case 'q':
puts("uit");
return EXIT_SUCCESS;
default:
puts("?");
};
};
}

--
Bart


 
Reply With Quote
 
Bill Reid
Guest
Posts: n/a
 
      03-08-2008

Jim <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> This has got to be really simple but I can't figure it out... I got
> swamped with work and am doing this project for a class that I'm
> taking and I can't figure out why the output is like this.
>
> I'm taking a Functional Programming in C and I have to write a
> shuffling program.


Ooooh, there's some "tricks" for that you should know about...but
since you didn't ask...

> The problem is that when I ask the user if they
> want to shuffle, they type y or n to answer. It then goes into a
> switch statement and executes code. It exits if they type no and is
> suppose to keep looping until they say no.


Yeah, this is the kind of stuff that will slow an "actual" programmer
(defined as somebody who really just wants to get a program written)
down to a crawl, because you have to incessantly re-invent this
little user input wheel, and about a dozen others...

I got so sick of repeating this stuff endlessly, I stuck them all in
a "library" (have you been taught that term?) so I now can simply
call a robust function with full user input error checking, rather
than watch my programs crash if I "fat-finger" something...but this
would get you tremendous extra credit if you show up with this...

> So... Here is the output I'm getting:
>
> -------------
> The array holds 20 elements which consists of:
> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
> 20 }
>
> Would you like to shuffle the array? (y=yes, n=no): y
> You have said you would like to shuffle.
> Shuffling...
>
> Would you like to shuffle the array? (y=yes, n=no): Would you like to
> shuffle the array? (y=yes, n=no):
> ----------------
>
> Notice that the question is getting asked twice instead of just once
> (after it loops). I added a default: line to my switch statement and
> it seems to be reading something else in the userval variable but I
> can't figure out what I'm doing wrong. Please help... This is due in
> 4 hours.


OK, I'm late, that's probably for the best, I shouldn't be doing your
homework for you...but here it is anyway!

-- Before anyone flames me, I didn't purposely
> procrastinate on this, I work full time, have a family and didn't get
> a chance to do this earlier.
>
> Here is my code:


Ah, forget it, here is the library function:

unsigned get_yn_input(const char *question_string,unsigned default_input) {
size_t string_size;

#ifdef __CONSOLE__
printf("%s",question_string);

if(default_input==DEF_YES) printf(" [{y}|n] ");
else if(default_input==DEF_NO) printf(" [y|{n}] ");
else printf(" [y|n] ");

GetInput :

fgets(yn_input,sizeof(yn_input),stdin);

string_size=strlen(yn_input);

if(yn_input[string_size-1]!='\n') {
fflush(stdin);
goto ReTry;
}

if(string_size>2)
goto ReTry;

if(IS_STR_SAME(yn_input,"y\n")) return YES;
else if(IS_STR_SAME(yn_input,"n\n")) return NO;
else if((default_input>NO_DEF)&&(yn_input[0]=='\n')) {
if(default_input==DEF_YES) return YES;
else if(default_input==DEF_NO) return NO;
}

ReTry :

printf("TRY AGAIN, just type 'y' or 'n'");
if(default_input>NO_DEF) printf(" \(or RETURN for default\)");
printf(": ");
goto GetInput;
#endif

#ifdef __WINGUI__
if(MessageDlg(question_string,
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo,0)==m rYes)
return YES;
else return NO;
#endif
}

Note that I can actually conditionally compile this into two separate
libraries: one for a "console" (terminal) type application, and another
for a "GUI" (guess which one!) application.

For the "console" application, the code performs all possible error
checking for fat-fingered idiotic input, and keeps prompting the user
until it gets either 'y' or 'n', which it then returns to the calling
function
(YES=1, NO=0, and don't ask about the other macro definitions,
they should be obvious).

For the "GUI" library, I just pop up a "dialog box" with the "question"
that prompts the user to press either the "Yes" or "No" button. The
GUI API does all the heavy lifting (at a heavy code bloat price!).

So if I want a "dual use" code module that I can also compile as
either a "console" or "GUI" application, I can just call the library
function
something like this:

get_yn_input("Would you like to shuffle the array?",DEF_NO)

And if it is a "console" application, the DEF_NO macro define tells
the function to interpret a RETURN without any input as a 'n' response.

So for your shuffling application, you could sort of use it like this:

void shuffle(void) {

PerformShuffle :

<shuffle code here>

if(get_yn_input("Would you like to shuffle the array again?",DEF_NO))
goto PerformShuffle;
}

Once again, the advantage of all this is that once you've got a robust
library function to do this routine crap, you can concentrate on the what
the program is really doing...but if you turned this in, your teacher would
know that you cheated...

---
William Ernest Reid



 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-08-2008
Bartc wrote:

>
> "santosh" <(E-Mail Removed)> wrote in message
> news:fqrush$r7l$(E-Mail Removed)...
>
> [Simple stdin input]
>> But it will block if stdin is already at end-of-file and if there are
>> no '\n' in the input buffer.
>>
>> For the long term using something like fgets followed by custom
>> parsing would be much more robust.
>>

>
> I was playing with the following code for reading a character at a
> time, to do the same sort of thing as the OP.
>
> But fgetc() doesn't return until end-of-line (Enter) is pressed. Is
> this normal behaviour?
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(void)
> {
> while (1)
> { switch (fgetc(stdin))
> { case 'Y': case 'y':
> puts("es");
> break;
> case 'N': case 'n':
> puts("o");
> break;
> case 'Q': case 'q':
> puts("uit");
> return EXIT_SUCCESS;
> default:
> puts("?");
> };
> };
> }


Yes. Because under most systems stdin is line buffered. Even if you turn
of it's buffering using setvbuf you'll still need to supply a
end-of-line since this is done at the OS level. You'll need to use a
more low-level API like <termios.h>. This is of course specific to a
system. Here is a UNIX example to read a single character without
echoing in non-canonical input mode (from glibc docs).

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

/* Use this variable to remember original terminal attributes. */

struct termios saved_attributes;

void
reset_input_mode (void)
{
tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
}
void
set_input_mode (void)
{
struct termios tattr;
char *name;

/* Make sure stdin is a terminal. */
if (!isatty (STDIN_FILENO))
{
fprintf (stderr, "Not a terminal.\n");
exit (EXIT_FAILURE);
}
/* Save the terminal attributes so we can restore them later. */
tcgetattr (STDIN_FILENO, &saved_attributes);
atexit (reset_input_mode);

/* Set the funny terminal modes. */
tcgetattr (STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
}
int
main (void)
{
char c;

set_input_mode ();

while (1)
{
read (STDIN_FILENO, &c, 1);
if (c == '\004') /* `C-d' */
break;
else
putchar (c);
}

return EXIT_SUCCESS;
}

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-08-2008
"Bartc" <(E-Mail Removed)> writes:

> I was playing with the following code for reading a character at a time, to
> do the same sort of thing as the OP.
>
> But fgetc() doesn't return until end-of-line (Enter) is pressed. Is this
> normal behaviour?


This topic is covered at some length in the C FAQ:

19.1: How can I read a single character from the keyboard without
waiting for the RETURN key? How can I stop characters from
being echoed on the screen as they're typed?

A: Alas, there is no standard or portable way to do these things in
C. Concepts such as screens and keyboards are not even
mentioned in the Standard, which deals only with simple I/O
"streams" of characters.

At some level, interactive keyboard input is usually collected
and presented to the requesting program a line at a time. This
gives the operating system a chance to support input line
editing (backspace/delete/rubout, etc.) in a consistent way,
without requiring that it be built into every program. Only
when the user is satisfied and presses the RETURN key (or
equivalent) is the line made available to the calling program.
Even if the calling program appears to be reading input a
character at a time (with getchar() or the like), the first call
blocks until the user has typed an entire line, at which point
potentially many characters become available and many character
requests (e.g. getchar() calls) are satisfied in quick
succession.

When a program wants to read each character immediately as it
arrives, its course of action will depend on where in the input
stream the line collection is happening and how it can be
disabled. Under some systems (e.g. MS-DOS, VMS in some modes),
a program can use a different or modified set of OS-level input
calls to bypass line-at-a-time input processing. Under other
systems (e.g. Unix, VMS in other modes), the part of the
operating system responsible for serial input (often called the
"terminal driver") must be placed in a mode which turns off line-
at-a-time processing, after which all calls to the usual input
routines (e.g. read(), getchar(), etc.) will return characters
immediately. Finally, a few systems (particularly older, batch-
oriented mainframes) perform input processing in peripheral
processors which cannot be told to do anything other than line-
at-a-time input.

Therefore, when you need to do character-at-a-time input (or
disable keyboard echo, which is an analogous problem), you will
have to use a technique specific to the system you're using,
assuming it provides one. Since comp.lang.c is oriented towards
those topics that the C language has defined support for, you
will usually get better answers to other questions by referring
to a system-specific newsgroup such as comp.unix.questions or
comp.os.msdos.programmer, and to the FAQ lists for these groups.
Note that the answers are often not unique even across different
variants of a system; bear in mind when answering system-
specific questions that the answer that applies to your system
may not apply to everyone else's.

However, since these questions are frequently asked here, here
are brief answers for some common situations.

Some versions of curses have functions called cbreak(),
noecho(), and getch() which do what you want. If you're
specifically trying to read a short password without echo, you
might try getpass(). Under Unix, you can use ioctl() to play
with the terminal driver modes (CBREAK or RAW under "classic"
versions; ICANON, c_cc[VMIN] and c_cc[VTIME] under System V or
POSIX systems; ECHO under all versions), or in a pinch, system()
and the stty command. (For more information, see <sgtty.h> and
tty(4) under classic versions, <termio.h> and termio(4) under
System V, or <termios.h> and termios(4) under POSIX.) Under
MS-DOS, use getch() or getche(), or the corresponding BIOS
interrupts. Under VMS, try the Screen Management (SMG$)
routines, or curses, or issue low-level $QIO's with the
IO$_READVBLK function code (and perhaps IO$M_NOECHO, and others)
to ask for one character at a time. (It's also possible to set
character-at-a-time or "pass through" modes in the VMS terminal
driver.) Under other operating systems, you're on your own.

(As an aside, note that simply using setbuf() or setvbuf() to
set stdin to unbuffered will *not* generally serve to allow
character-at-a-time input.)

If you're trying to write a portable program, a good approach is
to define your own suite of three functions to (1) set the
terminal driver or input system into character-at-a-time mode
(if necessary), (2) get characters, and (3) return the terminal
driver to its initial state when the program is finished.
(Ideally, such a set of functions might be part of the C
Standard, some day.) The extended versions of this FAQ list
(see question 20.40) contain examples of such functions for
several popular systems.

See also question 19.2.

References: PCS Sec. 10 pp. 128-9, Sec. 10.1 pp. 130-1; POSIX
Sec. 7.

19.2: How can I find out if there are characters available for reading
(and if so, how many)? Alternatively, how can I do a read that
will not block if there are no characters available?

A: These, too, are entirely operating-system-specific. Some
versions of curses have a nodelay() function. Depending on your
system, you may also be able to use "nonblocking I/O", or a
system call named "select" or "poll", or the FIONREAD ioctl, or
c_cc[VTIME], or kbhit(), or rdchk(), or the O_NDELAY option to
open() or fcntl(). See also question 19.1.

--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-08-2008
Bill Reid said:

> I shouldn't be doing your homework for you...


Right. Quite apart from the morality issue (and I haven't read the OP
closely enough to determine whether that /is/ an issue in this case)...

>
> fgets(yn_input,sizeof(yn_input),stdin);


....this reply fails to check the result of a function that is very likely
under some circumstances to fail. What's more, it attempts to populate a
non-existent array...

<snip>

> fflush(stdin);


....and this demonstrates that the author either doesn't know what fflush
does or doesn't know the difference between a stream open for output or
update and a stream open for input.

I can't help wondering whether, when the author of this code was at
college, someone else did /his/ homework.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments