Saturday, 15 January 2011

Switch Statement with three cases in C. Third Case is not running properly -



Switch Statement with three cases in C. Third Case is not running properly -

below do-while loop coded. when run it, first 2 cases job , run perfectly. however. 3rd case supposed quit programme instead nil , goes series of printf statements in origin of do-while loop. suggestions doing wrong?

do { printf("choose 1 of next (1, 2, or 3) \n"); printf("1. find gcd of 2 positive integers\n"); printf("2. sort 3 integers in ascending order\n"); printf("3. quit program\n"); printf("please come in choice: "); scanf("%d", &option); switch (option) { case 1: gcd(p, q); printf("\ndo want seek again? y(es) or n(o): "); getchar(); response = getchar(); break; case 2: sort(p, q, r); printf("\ndo want seek again? y(es) or n(o): "); getchar(); response = getchar(); break; case 3: break; } } while (response == 'y' || response == 'y'); //condition determine whether or not loop continues run. printf("\nthank using progam. goodbye!\n\n"); homecoming 0; }

response variable remains either y or y , while loop never exits.

add

response = 'x'; //or isn't y or y

before break; in case 3: option.

c switch-statement break do-while

No comments:

Post a Comment