-4

I am new to C++, and decided to make a simple calculator to help myself learn. I am having trouble adding a non-integer variable to the problem (in this case, a plus sign). How should go about doing this?

Here is the code:

#include <iostream>
using namespace std;
int main()
{
    char problem;
    int result;
    int a;
    int b;
    cin >> a;
    cin >> problem;
    cin >> b;
    result=a && problem && b;
            cout << result << endl ;
            return 0;
}
Dagoth Ur
  • 123
  • 5
  • I know there's a duplicate someone will find, but in short the problem is that the `Enter` key you press for the first input is stored in the input buffer as the newline character, which is then read for the second input. If you have stepped through the code line by line while watching the variables and their values, that would have been *very* obvious. – Some programmer dude Jul 13 '18 at 19:10
  • I always get the integer 1 for the answer. – Dagoth Ur Jul 13 '18 at 19:13
  • Have you tried entering 0 for a or b? 1 is the correct result for most evaluations of the expression 'a && problem && b' – Alexander Toptygin Jul 13 '18 at 19:16
  • 1
    And now I see that there are much more deep problems with your code. Please [get a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), what your doing with the ***logical and operator `&&`*** will not work the way you seemingly think it should. – Some programmer dude Jul 13 '18 at 19:16
  • 2
    Add the following information to your post. What input did you provide to the program? What results did you expect to see? What results did you see when the program was run? – R Sahu Jul 13 '18 at 19:19

2 Answers2

1

&& is not an arithmetic operator. It is logical and operator which returns a boolean value, either true or false in C++.

If you are familiar with logical operations, you should know that only true and true returns true. If any one of the operands of the logical and operator is false, then the entire expression is false.

Next, in C++, everything but 0, NULL (nullptr) and false is considered true, otherwise those three terms are false.

Therefore, in your experiment, I guess that you always entered non-zero elements so your expression is always true. And unlike other programming languages, for example, Python or Java, C++ won't automatically print out the text of boolean values. It would just show 0 for false and 1 for true. So in your demo you always see 1 on your screen.

hiimdaosui
  • 361
  • 2
  • 12
0

Here is a working version of the calculator without a user-defined operation: http://cpp.sh/7ckas

Now, if you want to let your user pick the operation, then you will need to do some more work. You can't just hack it together using '&&'. Instead, you need to create some kind of abstraction.

For example, you could ask the user, "Choose an operation: (1) plus (2) minus." Then then user types in a number corresponding to the operation that they want.

Then, you can use an if statement on that number. If 1, add the numbers. If 2, minus them.

Try it for yourself and let me know if you have any trouble getting it to work.

nyghtly
  • 222
  • 3
  • 8