c - Why am I getting the error "Break statement not within loop or or switch."? -
this simple program asks use's age , based on displays message.at end if it,the user asked if repeat whole thing again.but getting error
break statement not within loop or switch
when compile it. mean , how correct it?
#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 simple program using if else example. */ int age; while (printf("welcome, program designed if else statements.\n")); printf("please enter age.\n"); scanf (" %d", &age); /*enters age.*/ if (age < 18){ printf("you young!\n"); } else if (age > 18){ printf("ah you're old!\n"); } { printf(" woot.\n"); if (prompt_continue("do want try again? y/n") == 0) break; } return 0; }
just trying work through this, need little help. did use while loop wrong? thoughts helpful. thanks!
you need define scope of loop. in code:
while (printf("welcome, program designed if else statements.\n")); printf("please enter age.\n"); ... if (prompt_continue("do want try again? y/n") == 0) break;
what need is:
while (true) { printf("welcome, program designed if else statements.\n")); printf("please enter age.\n"); ... if (prompt_continue("do want try again? y/n") != 1) break; }
break
stops execution of while
loop here.
Comments
Post a Comment