L R Nix wrote:
> Cross_AM wrote:
>
>> #include <stdio.h>
>> int main(){
>> char c;
>> while(1){
>> printf(“Enter your choice
“);
>> scanf(”%c”, &c);
>>
>> }
>> return 0;
>> }
>>
>> This was the code I compiled. I wanted it to print the line and ask for
>> input but the result was something like the following
>>
>> Enter your choice
>> y
>> Enter your choice
>> Enter your choice
>
>>
>> Please explain why two lines are coming.
>
> You’ve asked scanf to read a single character (%c) at a time. So it reads
> the ‘Y’, and then the ‘ENTER’ after it. And the system doesn’t give your
> input to scanf until you press ENTER… it sees two characters. I imagine
> if you were to type a long line, you’d get a string of “Enter your choice”
> prompts running down the screen, one for each letter!
>
> If you want to read a whole line, you’ll need to have room for it (“char
> c” only defines a single character’s worth of space), and tell scanf to
> read a line (%s)
>
> ** Note ** scanf is not very friendly, and can contribute to buffer
> overruns
>
> A better function to use is fgets, as it allows you to read an entire
> ‘line’ of input, and has a maximum limit on how much it’ll grab, helping
> to prevent buffer overrun errors. (very bad things!)
>
> Try something like this:
>
> (Guys, don’t flame me, I’m working on the KISS principle here!)
>
> #include <stdio.h>
> int main(){
> char line[81]; /* extra for \0 at end /
> while(1){
> printf("Enter your choice
");
> fgets(line,80,STDIN);
> / your input will be in ‘line’ array /
> / and is limited to 80 chars maximum /
> / you can set max chars to anything
> * just remember to make sure there’s
> * room reserved for it */
> printf("You typed ‘%s’
",line);
> }
> return 0;
> }
>
> Admittedly, there are better ways to do this, but as a starting point…
> you’re doing great. Good luck.
>
>
Duh, replying to my own post too… I’m SUCH a blonde!
I just realized you might want to only read a single character… looks like
a menu prompt.
So this:
#include <stdio.h>
int main(){
int c; /* use ‘int’ to read characters */
while(1){
printf("Enter your choice
");
c=getchar();
}
return 0;
}
Is probably more what you’re looking for.
The reason you need ‘c’ to be an ‘int’ instead of a ‘char’ is that ANY input
function can return EOF, which technically is ‘-1’, but could be anything
really. EOF is usually out-of-range for a char variable, which can’t
represent the EOF and thus converts it to a char… meaning you would never
SEE an EOF… which is a real bummer when you’re actually looking for it.
You’ll probably want a
in there somewhere too… like this:
printf("Enter your choice: “);
c=getchar();
printf(”
");
printf("You pressed ‘%c’
",c);
aaaaannnd, now I’m rambling.
Hope that helps.
–
L R Nix
lornix@lornix.com