当用户输入"退出"时,如何结束该程序?我在循环之前和循环中尝试了一个if语句。我对编程很陌生,让我烦恼的是我无法解决这个问题。我甚至尝试过while while循环,但根本没用。
int main()
{
char word[30];
char yn;
int loopcount;
yn=0;
loopcount=0;
printf("Enter a word:");
scanf(" %s", word);
printf("Would you like to change the word? y=yes, n=no \n");
scanf(" %c", &yn);
if (yn=='n')
{
return 0;
}
while(yn>0)
{
printf("Enter a new word: \n");
scanf(" %s", word);
printf("New word is: %s \n");
loopcount= loopcount+1;
printf("You have changed the word %d times.\n", loopcount);
printf("Would you like to change the word? y=yes, n=no\n");
scanf(" %c", &yn);
if (yn=='n')
{
return 0;
}
return 0;
}
return 0;
}
答案 0 :(得分:3)
使用strcmp()
scanf(" %s", word);
if (strcmp(word, "quit") == 0) {
return 0;
}