Removing contents of variable in C -
i want delete contents of int variable when gets else statement.
the program requests number between 1 , 5 using scanf , number stored in int variable , if number isn't between 1 , 5 user directed else statement , have used goto statement take start , wondering how removed contents of variable during else statement don't create continuos loop.
with getchar it's fpurge(stdin). i'm running mac os x.
below code:
include
int main (int argc, const char * argv[]) {
int code;
start:
puts("please type error code."); puts("range 1-5: "); scanf("%d", &code); switch(code) { case 1: printf("information\n"); case 2: printf("information\n"); case 3: printf("information\n"); case 4: printf("information\n"); case 5: printf("information\n"); default: printf("information\n"); goto start; }
}
you looking do...while loop
edit don't forget break
statements!!
do { puts("please type error code."); puts("range 1-5: "); scanf("%d", &code); switch(code) { case 1: printf("information\n"); break; case 2: printf("information\n"); break; case 3: printf("information\n"); break; case 4: printf("information\n"); break; case 5: printf("information\n"); break; default: printf("invalid code\n");break; } } while(code<1 || code> 5);
Comments
Post a Comment