Код, который я пишу, - это, по сути, вопрос о том, чтобы попросить пользователя ввести там свое имя. Если имя пустое, т. Е. Пользователь забывает ввести там свое имя, тогда в коде будет упомянуто, что пользователь забыл ввести там имя, и спросить снова. Следует продолжать спрашивать, пока условие не будет выполнено.
// This sample compares user input to what is being typed. If the
// input is void of any charicters before pressing enter, it will print a reply.
#include <stdio.h>
#include <string.h>
int main()
{
char firstname[25];
printf("Please enter your first name:");
fgets(firstname,25,stdin);
// ask to enter in a name. If no name or text is present,
// will reply with no name entered
// and will loop untill a name is pressed and entered
do
{
printf("You pressed enter before entering your first name.\n Please enter first name");
}
while (firstname == NULL || strcmp(firstname,"")==0);
if(!strcmp(firstname, "firstname"))
{
printf("Thank you %s! for entering in your first name",firstname);
}
getchar();
}
Он зацикливается только один раз. Так что, не уверен, почему это не будет продолжаться, а также, разорвать цикл, чтобы сказать "thank you %s!
Кто-нибудь может привести другой пример, чтобы он работал, и я могу понять это лучше?