这个简单的程序询问用户的年龄并据此显示一条消息。如果是,最后询问用户是否想再次重复整个过程。但我收到错误
Break 语句不在循环或开关内
当我编译它时。这是什么意思,我该如何纠正?
#include <stdio.h>
#include <string.h>
static int prompt_continue (const char *prompt)
{
printf("%s", prompt);
char answer[5];
if (scanf("%1s", answer) != 1)
return 0;
if (answer[0] == 'y' || answer[0] == 'Y')
{
int c;
while ((c = getchar()) != EOF && c != '\n')
;
return 1;
}
return 0;
}
int main(void)
{
/*Creates a simple program using if else example. */
int age;
while (printf("Welcome, this program is designed for if else statements.\n"));
printf("Please enter your age.\n");
scanf (" %d", &age); /*Enters age.*/
if (age < 18){
printf("You are young!\n");
}
else if (age > 18){
printf("Ah you're old!\n");
}
{
printf(" Woot.\n");
if (prompt_continue("Do you want to try again? Y/N") == 0)
break;
}
return 0;
}
只是想解决这个问题,需要一点帮助。我用错了while循环吗?任何想法都会有所帮助。谢谢!