# Debugging Calculator

This is a discussion on Debugging Calculator within the C Programming forums, part of the General Programming Boards category; Hello everyone. I only recently started programming in C, and I have more recent still attempted to write a simple ...

1. ## Debugging Calculator

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);
else if(sel=='2')
{printf("Enter the numerator.\n");
gets(var1);
printf("Enter the denominator.\n");
gets(var2);
ans=atoi(var1/var2);
else if(sel=='3')
{printf("Enter the first number.");
gets(var1);
printf("Enter the second number.");
gets(var2);
ans=atoi(var1+var2);
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);
else if(sel=='5')
{printf("Enter the number you want to square.\n");
gets(var1);
ans=atoi(var1*var1);
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

2. >>char var1,var2;
These are char's, they are only one byte each, meaning you cannot store a word/sentence in them.
Try somthing like
>>char var1[100];
This can hold up to 99 characters, plus one byte remaining for the nul terminator.

>>getch("%i",sel);
getch() is non standard, you need to #include your compilers header for it. Maybe in your case that's conio.h

>>ans=atoi(var1*var2);
You can't do that Convert each char array to ints first, then multiply those.
num1 = atoi(myvar1);
num2 = atoi(myvar2);
ans = num1 * num2;

There are better ways than using atoi() too, but I guess we'll leave that for another day

3. Somthing you may want to think about also; INDENTING!!!
Not hard really, just helps others read your code, helps update it later on ect.. it hurts my eyes to read it as is, when I first started I didnt indent either but its really needed if your going to post code!

4. Originally posted by KrAzY CrAb
Somthing you may want to think about also; INDENTING!!!
It actually has to do with using code tags:
[code]
```int main( void )