I am writing a calculator code..
I am having issues with input validation.
I can't get in touch with my instructor as I am taking an online course.
I am in need of assistance....
I need to make it so my calculator forces the user to input integers for the menu options as well as for the calculations...
If they put a letter the program loops.
I need it to send an error to the user.
I have tried isdigit...but it isn't working.
i am new.
my code is below:
Code:
/*Midterm Project: Simple Calculator*/
#include <stdio.h>
#include <math.h>
int main(void)
{
int option,n1,n2,ans;
printf("\n\n\n\n\n Welcome to (name) Handy Calculator! \n\n\n\n\n\a");
printf(" Press any key to continue!\n");
getch();
do{
system("cls");
printf("\n\nPlease select an option from the list below.\n\n");
printf(" 1. Addition \n");
printf(" 2. Subtraction \n");
printf(" 3. Multiplication \n");
printf(" 4. Division \n");
printf(" 5. Exit \n\n");
scanf("%d",&option);
system("cls");
if(option==1)
{
printf("You have chosen Addition! Please select two numbers to add.\n");
printf("Enter first number...\n");
scanf("%d",&n1);
printf("Enter second number...\n");
scanf("%d",&n2);
ans=n1+n2;
printf("The answer is %d!\n\n",ans);
system("pause");
}
else if(option==2)
{
printf("You have chosen Subtraction! Please select two numbers to subtract.\n");
printf("Enter first number...\n");
scanf("%d",&n1);
printf("Enter second number...\n");
scanf("%d",&n2);
ans=n1-n2;
printf("The answer is %d!\n",ans);
system("pause");
}
else if(option==3)
{
printf("You have chosen Multiplication! Please select two numbers to multiply.\n");
printf("Enter first number...\n");
scanf("%d",&n1);
printf("Enter second number...\n");
scanf("%d",&n2);
ans=n1*n2;
printf("The answer is %d!\n",ans);
system("pause");
}
else if(option==4)
{
printf("You have chosen Division! Please select two numbers to divide.\n");
printf("Enter first number...\n");
scanf("%d",&n1);
printf("Enter second number...\n");
scanf("%d",&n2);
ans=n1/n2;
printf("The answer is %f!\n",ans);
system("pause");
}
else if(option==5)
{
exit();
}
}while(option!=5);
return 0;
}