This is a homework question. I am taking a C programming class. The requirement that I am working with now is
"If an empty message is entered, the user should be prompted to enter a non-empty message until they enter one which is at least 1 character long."
This is the code I have now:
int main(void)
{
char string[101];
int menu;
do
{
printf("Enter a text message (maximum 100 characters long) : ");
scanf(" %100[^\n]s", string);
}
while (string[0] == '\0');
If I take the space out in front of the % in the scanf line, and user just hits enter, the computer goes into an infinite loop and just prints gook to the screen. (The text on the screen is moving too fast for me to make sure, but I 'think' it is constantly printing the printf statement.)
With the space there, and the user just hits enter, it moves the cursor down to the next line until the user enters a valid string then moves on.
I want the question repeated every time the user just hits enter. I have tried empty for(;;), while and here you see the do-while variation.
Any comments welcome. Thanks.
!=and you're forcing the first letter to be a space so it would never exit. Maybe try strlen < 2 or remove the space - user34537