HELP !!
5 messages in this thread
Arrgh! HELP!! Save me from this creeping insanity PLEASE!!! What I'm
trying to do is have my program take a character from the keyboard, and put
it through a 'switch' routine. So here are my problems… 1) My C
programming stinks!
2) For reasons unknown the program goes right past any scanf, getchar, or
gets functions I put in. (One purpose for this is that I want to stop the
scroll so someone can read a page and then hit return to go to the next.)
3) I have tried `cases:' with single characters, integers, and strings, but
the program ALWAYS goes to the default: option passing my scanf like it
isn't there. Can I use anything other than single character 'a' cases?
I'm using a Manx 3.6a compiler. I'll show you what I've done so you can be
sure that I at least put it down right. Thanks. I'll put you in my will if
you save me from leaving C to learn to play the piano. some function() {
char input; printf ("Print some letter"); scanf ("%c", &input); switch (
input )
{
case 'e':
variable = this;
break;
case 'w':
variable = that;
break;
case "ne": /* <– just want to know if this is legal? */
function ();
break;
default:
/* this is the case that always gets chosen. */
break;
} }
Thanks for your time, regards Henry.
First, check a good book on C and meditate on it. 🙂 'case' labels must
be an integer, character, or constant expression. That means you "ne" is
illegal. It's a string constant, which works like a pointer to char, which
means you could do a 'strcmp( str, "ne" )' and test that result. First,
you'd never get 'ne' as the result from a single scanf("%c", …) after
all, because it only grabs a single character. If you did an scanf("%s",
…) then you could get a string of characters, and then use a cascading
series of 'if (strcmp( str, "e" ) == 0) { … } else if (strcmp( str, "ne"
) == 0) { … etc. This might solve some of your problems… I'm not quite
sure what you meant by "goes right past", so I won't comment on that.
Thanks for the help J.
I'm not to sure if I understand whta you said but I will try the strcmp()
and see if it helps.
As for the scanf, what I mean is that it actually passes right by it
without even waiting for something to be entered at the keyboard. Weird
huh?
Does getchar stop program flow until something is entered at the
keyboard? Cause if it does my program also ignores it too.
Best wishes Henry
All the functions that work on stdin (scanf, getchar, etc.) should pause
until an entire line has been entered (i.e., you press return), and will
then return/operate on whatever you entered. Do you have a short code
sample where getchar or scanf aren't waiting for your input?
-Mike
Henry,
Just a thought – your example program didn't have an
#include <stdio.h>
statement. Did you get any linker error messages about undefined pointers?
I've got Lattice 5.04, but I tried your example program (without the "ne"
section) and it did switch properly. I didn't need the include statment to
use a scanf(), but did need the include for using getchar(). Unfortunately,
it won't work as you intend when using stdin, since the CON: device buffers
characters until you hit return. (i.e. – I had to hit e<return> to get the
program to operate. Getting immediate keystrokes requires other techniques
which are not the present issue.
Hope this helps. 8)
David