1

Trying to write a simple calculater in C. I'm stuck in having the program terminated if letter 'A', 'B', 'C' or 'D' is not entered, otherwise continue. Even though I enter a valid character it never proceeds.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char letter;
    float num1,num2;
    printf("What operation would you like to perform?\n\tA) Addition\n\tB) Subtraction\n\tC) Multiplication\n\tD) Division\n\n");
    scanf("%c", &letter);

    if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D')
      printf("Not a valid operation\n");
        return 1;

    printf("Please enter first number: ");
    scanf("%f", &num1);
    printf("Please enter second number: ");
    scanf("%f", &num2);

    if (letter == 'A' || letter == 'a')
        printf("The sum of %f and %f, is %f\n", num1, num2, num1 + num2);

    else if (letter == 'B' || letter == 'b')
        printf("The difference of %f and %f, is %f\n", num1, num2, num1 - num2);

    else if (letter == 'C' || letter == 'c')
        printf("The product of %f and %f, is %f\n", num1, num2, num1 * num2);

    else if (letter == 'D' || letter == 'd')
        printf("The quoation of %f and %f, is %f\n", num1, num2, num1 / num2);
    else 
        printf("The operation was not valid");
    return 0;

}

Thank you for your help.

6
  • You have two 'B' clauses in your if-else ladder. Commented Sep 14, 2013 at 1:33
  • Thanks dreamlax. I just corrected that. The problem however still exists. Commented Sep 14, 2013 at 1:36
  • You may need to call fflush(stdout); after the printf's that don't include a \n at the end of the string in order to make them show up right away. Commented Sep 14, 2013 at 1:41
  • 4
    @holasz: Please don't edit your question to correct it for things people tell you in their answers, otherwise the answers make no sense at all when you read them. It's better to ask a new question, or at the minimum add a new section prefaced "EDIT:" so the original code is still visible. Commented Sep 14, 2013 at 1:52
  • You've got a problem with your quoation (which should be a 'quotient' in English), in that the code will never be executed because you copied and pasted and forget to edit the B and b into D and d. We can understand what you intended; compilers won't. Commented Sep 14, 2013 at 1:54

3 Answers 3

3
if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D')
  printf("Not a valid operation\n");
    return 1;

This part is the problem. Although return 1; is indented, it will execute regardless because it is not part of the if block. Additionally, you are using the wrong operator, your condition statement reads "if letter is not A or letter is not B ..." which is always going to be true because letter cannot be both A and B at the same time. You need to envelope the two statements and use the && operator instead, like this:

if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
{
    printf("Not a valid operation\n");
    return 1;
}

In C, indentation is meaningless to the compiler. If you need to execute multiple statements as a result of a condition, they must be wrapped up into a compound statement using { and }.

Sign up to request clarification or add additional context in comments.

13 Comments

Additionally, if the user types in a, b, c, or d, then the first if block, even after your correction, will reject it. But your larger point, always use braces, is correct.
You need to replace || with &&, otherwise the condition is always true (letter cannot be equal to more than one char at a time, so even if one of the conditions is false, the other tree are going to be true).
I updated the post. THe problem is still there. Even surrounding it with brackets. {..}
@holasz look at what dasblinkenlight said and think about how || works. The problem will become apparent.
@holasz: use if ( tolower(letter) == 'a' ) or similar. C has no standard regex support, although C++ now does.
|
0

You check

 if (letter != 'A' || letter != 'B' ...)

Ok - if letter is 'B', then it is not A, and the program stops testing there and prints your failure condition and returns.

On the other hand, if letter was 'A', it would not be 'B', and so it would fail the second test instead, and fail there.

What you want:

if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')

Alternately, you could use the C function "strchr" which searches for a character in a string.

if (!strchr("ABCDabcd", letter)) // returns NULL, which is false, if no match
{
    printf("Invalid operation");
    return 1;
}

Comments

0

Try:

if (letter != 'A' && letter != 'B' && letter != 'C' && letter != 'D')
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.