0

My Task is a simple Boolean Calculator

I want to make a simple boolean calculator, which uses OR, AND, NAND and XOR. This is what it has to do:

If the user enters a string starting with q (like quit) at any time, the program should immediately exit. If the user enters a string starting with c (like cancel) at any time, the program should abort the current calculation and ask for the first argument again. If the user enters anything other than a 0, a 1, or a string starting with c or q, you should display an error (see below) and ask for the same value again.

Now, I know this is probably very silly, but I really have started right now programming...

It doesn't matter what I enter, it only retuns the "You chose to quit!" Why does it "ignore" the switch?

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

int main() {
    // Declaration Part
    char user_input[100];

    //Main Body
    gets(user_input);
    while (user_input[0] == !'q') {
        switch (user_input[0]) {
          case '0':
          case '1':
            printf("You chose boolean algebra! \n");
            break;
          case 'c':
            printf("%s\n", "You chose to continue!");
            continue;
        }
    }
    printf("You chose to quit! \n");
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Mr PC
  • 29
  • 1
  • 3
    `while (user_input[0] == !'q')` ==> `while (user_input[0] != 'q')` – Weather Vane Nov 13 '16 at 14:50
  • Compile with all warnings & debug info (eg `gcc -Wall -g` if using [GCC](http://gcc.gnu.org/)...), improve your code till you get no warnings, then **use the debugger** (`gdb`), e.g. to run the program step by step. – Basile Starynkevitch Nov 13 '16 at 14:53
  • 1
    BTW `gets` is obsolete and dangerous. Use [fgets](http://en.cppreference.com/w/c/io/fgets) instead. Please read the [documentation](http://en.cppreference.com/w/c) of *every* function that you are using. – Basile Starynkevitch Nov 13 '16 at 14:57
  • [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714) – phuclv Nov 13 '16 at 17:18

3 Answers3

0

There are 2 major problems in your code:

  • The condition while (user_input[0] == !'q') should be written while (user_input[0] != 'q')

  • The obsolete function gets() should never be used. Use fgets() instead.

  • You do not read a new line in the while loop... the condition is constant, you have either an infinite loop (if you type enter directly) or one you never enter.

Here is a better version:

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

int main(void) {
    // Declaration Part
    char input[100];

    //Main Body
    while (fgets(input, sizeof input, stdin) != NULL && input[0] != 'q') {
        switch (input[0]) {
          case '0':
          case '1':
            printf("You chose boolean algebra! \n");
            break;
          case 'c':
            printf("%s\n", "You chose to continue!");
            continue;
        }
    }
    printf("You chose to quit!\n");
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

The operator ! is Boolean, if the operand is non-zero, the result is false which implicitly casts to zero in your condition expression:

user_input[0] == !'q'

You are simply comparing user_input[0] to zero so the switch is never entered. That is to say it is not switch that is not doing what you want - it is the incorrect Boolean expression in the if(...) that precedes it.

What you need is the binary operator !=

user_input[0] != 'q'
Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Note as stated in other answers this code has other issues - but they were not part of your question, so I have deliberately not addresses those. – Clifford Nov 13 '16 at 15:38
0

Why does it "ignore" the switch?

It is not executing the switch because it is inside a conditional block for which the condition is never true. You are addressing the wrong part of the code; why it does not execute the switch should be obvious, the question you should then ask is rather "Why is the while condition never true?".

The operator ! is boolean, if the operand is non-zero, the result is false which implicitly casts to zero when compared to an integer. In your condition expression:

user_input[0] == !'q'

you are simply comparing user_input[0] to zero so the loop body and therefore the switch is never entered. So it is not switch that is not doing what you want - it is the incorrect boolean expression in the while(...) that precedes it.

What you need is the binary operator !=

user_input[0] != 'q'
Clifford
  • 88,407
  • 13
  • 85
  • 165