Hi guys,
I'm writing a program that calculates resistance values from the colour codes. My program works fine, but I am having trouble catching user errors.
My program should print an error message, when the user enters an invalid value, then prompt the user to try again.
Basically, my question is how to implement this. I tried using while loops, but ended up with an infinite loop of error messages and user prompts.
I'm not looking for people to do my homework, but can somebody show me an example of a loop that catches errors?
Here is my code:Thank you,Code:#include <stdio.h> #include <math.h> void main() { int user_choice, choice1, choice2, choice3, choice4, band_1, band_2; int band_3, band_4; float resistance; printf("Please choose an option:\n"); printf("\t 1. Calculate resistance\n"); printf("\t 2. Help\n"); printf("\t 3. EXIT\n"); do { printf("Enter Selection:"); scanf("%d", &user_choice); }while (user_choice !=1 && user_choice!=2 && user_choice!=3); if (user_choice==1) { printf("\nPlease enter the colour of the first band\n"); printf("1 for Black\t 2 for Red\t 3 for Brown\n"); printf("4 for Orange\t 5 for Yellow\t 6 for Green\n"); printf("7 for Blue\t 8 for Violet\t 9 for Gray\n"); printf("10 for White.\n"); scanf("%d", &choice1); if (choice1==1) { band_1=0; } if (choice1==2) { band_1=1; } if (choice1==3) { band_1=2; } if (choice1==4) { band_1=3; } if (choice1==5) { band_1=4; } if (choice1==6) { band_1=5; } if (choice1==7) { band_1=6; } if (choice1==8) { band_1=7; } if (choice1==9) { band_1=8; } if (choice1==10) { band_1=9; } printf("\nPlease enter the colour of the second band\n"); printf("1 for Black\t 2 for Red\t 3 for Brown\n"); printf("4 for Orange\t 5 for Yellow\t 6 for Green\n"); printf("7 for Blue\t 8 for Violet\t 9 for Gray\n"); printf("10 for White.\n"); scanf("%d", &choice2); if (choice2==1) { band_2=0; } if (choice2==2) { band_2=1; } if (choice2==3) { band_2=2; } if (choice2==4) { band_2=3; } if (choice2==5) { band_2=4; } if (choice2==6) { band_2=5; } if (choice2==7) { band_2=6; } if (choice2==8) { band_2=7; } if (choice2==9) { band_2=8; } if (choice2==10) { band_2=9; } printf("\nPlease enter the colour of the third band\n"); printf("1 for Black\t 2 for Red\t 3 for Brown\n"); printf("4 for Orange\t 5 for Yellow\t 6 for Green\n"); printf("7 for Blue\t 8 for Violet\t 9 for Gray\n"); printf("10 for White\t 11 for Gold\t 12 for Silver\n"); scanf("%d", &choice3); if (choice3==1) { band_3=0; } if (choice3==2) { band_3=1; } if (choice3==3) { band_3=2; } if (choice3==4) { band_3=3; } if (choice3==5) { band_3=4; } if (choice3==6) { band_3=5; } if (choice3==7) { band_3=6; } if (choice3==8) { band_3=7; } if (choice3==9) { band_3=8; } if (choice3==10) { band_3=9; } if (choice3==1) { band_3=-1; } if (choice3==12) { band_3=-2; } resistance=((band_1*10)+band_2)*pow(10, band_3); if (resistance >=1000.0 && resistance<100000.0) { resistance=resistance/1000.0; printf("\nThe resistance is %f kilo Ohms", resistance); } if (resistance>=1000000.0 && resistance<1000000000.0) { resistance=resistance/1000000; printf("\nThe resistance is %f Mega Ohms"); } printf("\n\nthis is the resistance %f ohms", resistance); } if (user_choice==2) { printf("To use this program enter the resistor colour codes using the numbers 1-12\n"); } if (user_choice==3) { printf("Good day"); } }
-cda67



LinkBack URL
About LinkBacks


