Thread: calculator problem

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    calculator problem

    I have a problem with a calculator i was making. I could use some help because i get the following error:

    calculator.c: In function ‘main’:
    calculator.c:14: error: ‘a’ undeclared (first use in this function)
    calculator.c:14: error: (Each undeclared identifier is reported only once
    calculator.c:14: error: for each function it appears in.)
    calculator.c:24: error: ‘s’ undeclared (first use in this function)
    calculator.c:34: error: ‘m’ undeclared (first use in this function)
    calculator.c:44: error: ‘d’ undeclared (first use in this function)

    here is the code:

    Code:
    #include <stdio.h>
    
    int main ()
    {
    char letter;
    int num1;
    int num2;
    int answer;
    
    printf("Enter <a> for addition \nEnter <s> for subtraction               \nEnter <m> for multiplication \nEnter <d> for division \nEnter:");
    
    scanf("%c", &letter);
    
    if (letter == a)
    {
    printf("Enter the two numbers you want to add");
    scanf("%d", &num1);
    scanf("%d", &num2);
    
    answer = num1+num2;
    printf("The answer is:%d", answer);
    }
    
    else if (letter == s)
    {
    printf("Enter the two numbers you want to subtract");
    scanf("%d", &num1);
    scanf("%d", &num2);
    
    answer = num1-num2;
    printf("The answer is:%d", answer);
    }
    
    else if (letter == m)
    {
    printf("Enter the two numbers you want to multiply");
    scanf("%d", &num1);
    scanf("%d", &num2);
    
    answer = num1*num2;
    printf("The answer is:%d", answer);
    }
    
    else if (letter == d)
    {
    printf("Enter the two numbers you want to divide");
    scanf("%d", &num1);
    scanf("%d", &num2);
    
    answer = num1/num2;
    printf("The answer is:%d", answer);
    }
    else 
    {
    printf("Please Try again");
    }
    
    return 0;
    }
    Could someone give me a hand please?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should use single quotes since they are character literals, e.g., 'a'.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to indent your code, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    You should use single quotes since they are character literals, e.g., 'a'.
    Thanks that worked!

  5. #5
    Registered User ungalnanban's Avatar
    Join Date
    Feb 2010
    Location
    Chenai
    Posts
    12

    Thumbs up single quote is needed

    in your program you need to use the single quote.
    because the variable type "letter" is char. your comparing the values using == . so it needs the ASCII value
    if you use the switch statement that time you will use the case values in single quote for alphabets,
    for integer values no need. the same things here happen.

    so put single quote for 'a','s','m' and 'd'.
    Last edited by ungalnanban; 02-18-2010 at 01:19 AM. Reason: gramer mistakes.

Popular pages Recent additions subscribe to a feed