• 07-09-2011
calibean
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;                 }```
• 07-09-2011
quzah
Take a look at this thread: http://cboard.cprogramming.com/c-pro...ror-proof.html

We went over reading input here fairly well.

Quzah.
• 07-09-2011
calibean
i'm sure you did...
I need help and direction.
please don't respond unless you will actually help me.
i don't know any programmers that i could even ask for assistance....
• 07-09-2011
quzah
So you are too lazy to click and read a link on the same forum asking the same type of question you are asking? Yeah... have fun helping yourself.

Quzah.
• 07-09-2011
calibean
take a hint and get lost!!!...i have been reading..i'm new and a little confused...
• 07-09-2011
calibean
oh and... i did try the switch..if i knew what i was doing wrong..i would fix it and not ask for help...
• 07-09-2011
AndrewHunter
Quote:

Originally Posted by calibean
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....

First off quzah did point you in the right direction. Secondly your program is looping if the user inputs anything besides 1-5 because you tell it to:
Code:

```.... }while(option!=5);```
Note in the discussion quzah posted it discussed using the return value from scanf(). Take a look at this simple example:
Code:

```int main(void){         int num, read;         printf("Enter a number:");         read = scanf("%d", &num);         printf("%d", read);         getchar();         getchar();         getchar();         return (0); }```
Guess what gets printed if the user enters a character?
• 07-09-2011
calibean
Thank you. I don't mean to be rude. I'm just frustrated
I did look at what he pointed me to... but alot of this stuff is still foreign to me.

I need someone who will patiently help me and not be cocky and rude about it.

I'm sorry Quzah.
• 07-09-2011
CommonTater
Quote:

Originally Posted by calibean
take a hint and get lost!!!...i have been reading..i'm new and a little confused...

Wow... you gotta work on that temper.

We are NOT going to hand you finished code... you won't learn a thing that way.

The answer to your problem lies in checking the return value of scanf() ... it returns the number of successful conversions (inputs). What do you think it returns if the user enters an unconvertable input?
• 07-09-2011
AndrewHunter
Ok, so right here in your code is where you are taking user input and it could be where you could do the initial error check for your input:
Code:

```scanf("%d",&option); system("cls");```
So, IF you wanted to check your input and then PRINT and error message how would you think you would go about it. I am sure that you ran the above sample code I posted and by now realize that scanf returns the number of successful conversions it performs.
• 07-09-2011
calibean
Thank you Andrew.
I apparently need a mentor...and after my midterm...a vacation. haha
• 07-09-2011
AndrewHunter
No worries. You should have what information you need now to get the program working. The continue keyword would be something to take a look at as well.