Hello everyone. I only recently started programming in C, and I have more recent still attempted to write a simple 'calculator' program, one that solves simple addition, subtraction, division, etc. problems. It's got numerous problems, but I don't know how to fix them.
First, when I make a choice at the menu selection, I always get the 'else' statement, not an 'if' or 'else if.' This holds true even when my selection is from 1 to 5. What?!
Then, I get an error at the getting the second number. I don't have a clue as to why.
Here's the code. It's 45 lines long, I think...
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sel,ans,which;
char var1,var2;
printf("Specify the type of mathematical problem you wish to do.\n1=multiplication\n2=division\n3=addition\n4=s ubtraction\n5=squaring a number\nPress the number that corresponds to your inquiry.\n");
getch("%i",sel);
if(sel=='1')
{printf("Enter the first number.\n");
gets(var1);
printf("Enter the second number.\n");
gets(var2);
ans=atoi(var1*var2);
printf("The answer is %i.\n",ans);}
else if(sel=='2')
{printf("Enter the numerator.\n");
gets(var1);
printf("Enter the denominator.\n");
gets(var2);
ans=atoi(var1/var2);
printf("The answer is %i.\n",ans);}
else if(sel=='3')
{printf("Enter the first number.");
gets(var1);
printf("Enter the second number.");
gets(var2);
ans=atoi(var1+var2);
printf("The answer is %i.\n",ans);}
else if(sel=='4')
{printf("Enter the first number.\n");
gets(var1);
printf("Enter the number you want to subract from the first.\n");
gets(var2);
ans=atoi(var1-var2);
printf("The answer is %i.\n",ans);}
else if(sel=='5')
{printf("Enter the number you want to square.\n");
gets(var1);
ans=atoi(var1*var1);
printf("The answer is %i.\n",ans);}
else
{printf("You fruit! There were only five choices!\n");}
system("PAUSE");
return 0;
}
If any of you out there see anything simple and wonder why I didn't see it, it's because I am a beginner. Help!!
Raytro2