Thread: Debugging Calculator

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Unhappy 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);
    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    70
    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!
    "...since anyone who is anyone knows C..." -Peter Cellik

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by KrAzY CrAb
    Somthing you may want to think about also; INDENTING!!!
    It actually has to do with using code tags:
    [code]
    ... your code here
    [/code]

    Which gives it the look of:
    Code:
    int main( void )
    {
        return printf("Nice, readable code.");
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  2. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM