EvilGuru
asked on
Scanf Blank Input
Hi, in my program I need to get the users input, so I did all of the normal stuff, created a variable and then did scanf to fill it with the users input. However, it will not let the input be 'blank'. When I ask the user to type in their team name I want to give them the option of leaving it blank, rather than having to type in something (scanf will not take an input until a char other than crlf (eg the enter key) has been typed. How can I make it so that if the user does not want to type something in they can just press enter and have it go onto the next input.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main () {
char input[80];
printf("Please type in your team: ");
scanf("%s", &input);
if (input == "") strcpy(input, "default"); //set the team to default if left blank
system("pause");
return 0;
}
As you can see it wont let them have a blank input, they have to try some kind of char, can anyone help me?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main () {
char input[80];
printf("Please type in your team: ");
scanf("%s", &input);
if (input == "") strcpy(input, "default"); //set the team to default if left blank
system("pause");
return 0;
}
As you can see it wont let them have a blank input, they have to try some kind of char, can anyone help me?
if (strlen(input) == 0) ...
ASKER
The problem is with scanf, compile the program and at the prompt press enter, it just beings the cursor down a line. You can press it as many times as you want and the same thing will happen. The press a character (eg. f) then enter and it goes onto the next line of the code. It seems as if scanf waits for a non-newline input before moving onto the next line of the code and that is my problem.
ASKER
I have found that if I use the evil 'gets();' function it works fine. However I am sure we all know about the buffer overun problems with gets, so I dont really want to use it for that reason.
But
char input[80];
gets(input);
if((strlen(input)) == 0) strcpy(input, "default");
Does work. Anyone know of a 'safer' version of gets?
But
char input[80];
gets(input);
if((strlen(input)) == 0) strcpy(input, "default");
Does work. Anyone know of a 'safer' version of gets?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Otherwise you can read character by character. check if character is '\n' come out of loop.
you need to limit your scanf too.
int main () {
char input[80];
printf("Please type in your team: ");
scanf("%79s", &input);
}
MageDribble
int main () {
char input[80];
printf("Please type in your team: ");
scanf("%79s", &input);
}
MageDribble
Otherwise you can write as
scanf("%[^\n]",str); I had checked, this is working fine as what you expected.
-Mahesh
scanf("%[^\n]",str); I had checked, this is working fine as what you expected.
-Mahesh
ASKER
Smpoojary that is amazing! Could you explain how it works? Also is there a way using that method to limit the number of chars (like scanf(%79s,&input) ) to stop buffer overuns?
>>>Could you explain how it works?
[^\n] This is like a regular expression. Means read any character except '\n'. If '\n' comes scanf reads and discards it.
^ == not
>>>Also is there a way using that method to limit the number of chars (like scanf(%79s,&input) ) to stop buffer overuns?
Try for scanf("%79[^\n]",str);
[^\n] This is like a regular expression. Means read any character except '\n'. If '\n' comes scanf reads and discards it.
^ == not
>>>Also is there a way using that method to limit the number of chars (like scanf(%79s,&input) ) to stop buffer overuns?
Try for scanf("%79[^\n]",str);
ASKER
I thought that the caret char (^) means 'starts-with' in regexp?
Yes, beginning of the pattern.
But in C, "don't read the following characters"
-Mahesh
But in C, "don't read the following characters"
-Mahesh
The problem isn't scanf(), it's your null test.
if (input == "")
isn't valid C. Oh, it's legal C, but it doesn't do what you want. It tests to see if the address of input is equal to the address of an empty string. Since the two are clearly stored at different addresses, you'll always get FALSE.
Try this:
if (strcmp (input, "\n") == 0) // See if only the new-line character is returned
Good Luck!
Kent