Hello,
My father and I have made this calculator. It works, but I want to include data validation so that if the user does not enter 'y' to continue or 'q' to quit, it will tell the user that he has made an error and prompt the user to try again.
I've tried using a "do...while" and a "while" but cannot get it to work.
Also, ignore my fathers abuse of "else". He likes to use it because it's easier for him to read because he was a COBOL programmer for years.
Any help would be appreciated.
Code:#include <stdio.h> #include <stdlib.h> int main(void) { double num1 = 0; double num2 = 0; char operand[1]; char exitsw = ' '; do { printf ("\nKey 1st Number, +, -, *, or /, 2nd Number, and Press Enter.\n\n"); scanf("%lf %c %lf", &num1, operand, &num2); if (operand[0] == '+') printf ("Sum of two numbers is %f \n", num1+num2); else if (operand[0] == '-') printf ("Difference between two numbers is %f \n", num1-num2); else if (operand[0] == '*') printf ("Product of two numbers is %f \n", num1*num2); else if (operand[0] == '/' && num2 == 0) printf ("Resubmit: Zero Divisor Is Not Valid \n"); else if (operand[0] == '/') printf ("Division of two numbers is %f \n", num1/num2); else printf ("Resubmit: Invalid Operand \n"); fflush(stdin); printf ("\nWould you like to make another calculation?\n"); printf ("\nTo make another calculation, enter Y.\n"); printf ("\nTo Quit, enter q.\n"); scanf("%c", &exitsw); }while (exitsw == 'y' || exitsw == 'Y'); return 0; }



4Likes
LinkBack URL
About LinkBacks



We'll have to up our keyboarding rate to catch "fast fingers" Tim, though.
