Thread: Debugging Calculator

  1. #1
    Registered User
    Join Date
    Feb 2003

    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");
    {printf("Enter the first number.\n");
    printf("Enter the second number.\n");
    printf("The answer is %i.\n",ans);}
    else if(sel=='2')
    {printf("Enter the numerator.\n");
    printf("Enter the denominator.\n");
    printf("The answer is %i.\n",ans);}
    else if(sel=='3')
    {printf("Enter the first number.");
    printf("Enter the second number.");
    printf("The answer is %i.\n",ans);}
    else if(sel=='4')
    {printf("Enter the first number.\n");
    printf("Enter the number you want to subract from the first.\n");
    printf("The answer is %i.\n",ans);}
    else if(sel=='5')
    {printf("Enter the number you want to square.\n");
    printf("The answer is %i.\n",ans);}
    {printf("You fruit! There were only five choices!\n");}
    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!!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>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() is non standard, you need to #include your compilers header for it. Maybe in your case that's conio.h

    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
    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
    Originally posted by KrAzY CrAb
    Somthing you may want to think about also; INDENTING!!!
    It actually has to do with using code tags:
    ... your code here

    Which gives it the look of:
    int main( void )
        return printf("Nice, readable code.");
    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