How do I return a printf statement if the user inputs nothing?
I want to execute a statement based on the input of the user:
#include <stdio.h>
#include <string.h>
void main() {
char string_input[40];
int i;
printf("Enter data ==> ");
scanf("%s", string_input);
if (string_input[0] == '\n') {
printf("ERROR - no dat开发者_Python百科a\n");
}
else if (strlen(string_input) > 40) {
printf("Hex equivalent is ");
}
else {
printf("Hex equivalent is ");
}
}
When I run it, and just press enter, it goes to a new line instead of saying "ERROR - no data".
What do I do?
CANNOT USE FGETS as we have not gone over this in class.
Use
char enter[1];
int chk = scanf("%39[^\n]%c", string_input, enter);
but string_input
will not have a '\n'
inside. Your test
if (string_input[0] == '\n') {
printf("ERROR - no data\n");
}
will have to be changed to, for example
if (chk != 2) {
printf("ERROR - bad data\n");
}
use fgets
instead of scanf
. scanf
doesn't check if user enters a string longer than 40 chars in your example above so for your particular case fgets
should be simpler(safer).
Can you use a while loop and getch, then test for the <Enter>
key on each keystroke?
scanf
won't return until it sees something other than whitespace. It also doesn't distinguish between newlines and other whitespace. In practice, using scanf
is almost always a mistake; I suggest that you call fgets
instead and then (if you need to) use sscanf
on the resulting data.
If you do that, you really ought to deal with the possibility that the user enters a line longer than the buffer you pass to fgets
; you can tell when this has happened because your entire buffer gets filled and the last character isn't a newline. In that situation, you should reallocate a larger buffer and fgets
again onto the end of it, and repeat until either you see a newline or the buffer gets unreasonably large.
You should really be similarly careful when calling scanf
or sscanf
-- what if the user enters a string 100 characters long? (You can tell scanf
or sscanf
to accept only a limited length of string.)
On the other hand, if this is just a toy program you can just make your buffer reasonably long and hope the user doesn't do anything nasty.
fgets
does what you need. Avoid using scanf
or gets
. If you can't use fgets
try using getchar
The problem is that "%s"
attempts to skip white-space, and then read a string -- and according to scanf
, a new-line is "whitespace".
The obvious alternative would be to use "%c"
instead of "%s"
. The difference between the two is that "%c"
does not attempt to skip leading whitespace.
A somewhat less obvious (or less known, anyway) alternative would be to use "%[^\n]%*[\n]"
. This reads data until it encounters a new-line, then reads the new-line and doesn't assign it to anything.
Regardless of which conversion you use, you want (need, really) to limit the amount of input entered so it doesn't overflow the buffer you've provided, so you'd want to use "%39c"
or "%39[^\n]"
. Note that when you're specifying the length for scanf
, you need to subtract one to leave space for the NUL terminator (in contrast to fgets
, for which you specify the full buffer size).
What platform are you running on?
Is the character sent when your press the ENTER key actually '\n'
, or might it be '\r'
? Or even both one after the other (ie. "\r\n"
).
精彩评论