calculator problem

This is a discussion on calculator problem within the C Programming forums, part of the General Programming Boards category; I have a problem with a calculator i was making. I could use some help because i get the following ...

  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
    21,431
    You should use single quotes since they are character literals, e.g., 'a'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    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 12:19 AM. Reason: gramer mistakes.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21