-2

The calculator program will accept inputs for the 1st number, operation, and 2nd number.

I've set up the program to select the operation with conditional statements. The program does not output however.

I also tried to print out an error message for improper operation selection using xor logic.

I think the problem lies with how I'm using my variables. Any critique or advice is appreciated.

I'm new to learning C++, so I'm trying to make a simple two input calculator. I'm trying to challenge my understanding of variables and conditional statements.


void add_func();
void sub_func();
void mult_func();
void divi_func();

int main()
{
        double a;
        double b;
        char op;

        cout << "Input 1st Number:" << endl;
        cin >> a;
        cout << "Select Operation: (1 = +) (2 = -) (3 = *) (4 = /)" << endl;
        cin >> op;
        cout << "Input 2nd Number:" << endl;
        cin >> b;

        if(op == 1)
                cout << add_func();
        if(op == 2)
                sub_func();
        if(op == 3)
                mult_func();
        if(op == 4)
                divi_func();
        if(op != (1 ^ 2 ^ 3 ^ 4))
                cout << "Invalid Operation Selected" << endl;

        return 0;
}

void add_func()
{
        double a;
        double b;
        double c;

        c = a + b;
        cout << a << " + " << b << " = " << c << endl;
}

I've truncated the code to include only the add function. It should be adding the numbers and printing out the result.

Pilf
  • 7
  • 4
    The local variables in `add_func()` are completely unrelated to the variables declared in `main()`. It seems you need to get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn about the basics. – πάντα ῥεῖ Jun 19 '19 at 21:07
  • Also with `op` being `char` then `cin >> op` inputs a single character and then you compare its ASCII value to 1,2,3,4. So, the only way to select an addition operation would be to type Control+V Control+A (on Unix systems at least). – Daniel Schepler Jun 19 '19 at 21:50
  • Something like `if(op == 1)` won't fly. you either need to do `if(op == '1')`, or `op` needs to be an int instead. –  Jun 19 '19 at 21:59

3 Answers3

2

I'm pretty sure this: if(op != (1 ^ 2 ^ 3 ^ 4)) isn't doing what you think it is.

The ^ operator is a bitwise exclusive-OR, which gives a 0 bit in the result if both inputs are 0, or if both inputs are 1. If one input is a 1 and the other is a 0, the result is a 1.

The operations are evaluated left to right, so let's work through them one at a time. To make it apparent how a result comes about, we'll include the binary equivalents of the numbers:

  • 1 ^ 2 -> 0001 ^ 0010 -> 0011, which is 3
  • 3 ^ 3 -> 0011 ^ 0011 -> 0000, which is 0
  • 0 ^ 4 -> 0000 ^ 0100 -> 0100, which is 4

So your code is equivalent to: if (op != 4).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

You need to use function arguments to pass values between functions. Two variables in different functions are never the same variable even when they have the same name.

And you have to choose whether you are going to do your cout << inside your main function, or inside your add function. You seem to be trying to do both.

Here's how to do it (I choose to do cout << in the add function not the main function).

void add_func(double x, double y);

...

if(op == 1)
    add_func(a, b);

...

void add_func(double x, double y)
{
    double sum = x + y;
    cout << x << " + " << y << " = " << sum << endl;
}

See the parameters in the add function (which I've called x and y, you can call them what you like). And see the a and b variables are passed as arguments when the add function is called.

Your C++ book must talk about function parameters and passing arguments, it's absolutely fundamental to C++ programmming. Time to revise that chapter I think.

john
  • 85,011
  • 4
  • 57
  • 81
  • _"You need to use function arguments to pass values between functions."_ Well, variables at global scope could do the same, not that I'd recommend to do so :-P ... – πάντα ῥεῖ Jun 19 '19 at 21:13
0

please consider using a simple online tutorial to learn the basics. For example: https://www.tutorialspoint.com/cplusplus/index.htm or: https://www.w3schools.com/cpp/default.asp Also, when writing code, try to to pick the simplest solution. It would get complicated later anyway :) For a purpose of a simple calculator, a simple switch would suffice. Please consider the following C code:

#include <iostream>

int main()
{
    double a;
    double b;
    char op;

    while(true)
    {
        std::cout << "Input 1st Number: " << std::endl;
        std::cin >> a;
        std::cout << "Select Operation: (+) (-) (*) (/) or (x) to exit: " << std::endl;
        std::cin >> op;
        std::cout << "Input 2nd Number: " << std::endl;
        std::cin >> b;

        switch (op)
        {
        case '+':
            std::cout << std::to_string(a+b) << std::endl;
            break;
        case '-':
            std::cout << std::to_string(a-b) << std::endl;
            break;
        case '*':
            std::cout << std::to_string(a*b) << std::endl;
            break;
        case '/':
            std::cout << std::to_string(a/b) << std::endl;
            break;
        case 'x':
            return 0;
            break;        
        default:
            std::cout << "Invalid Operation Selected" << std::endl;
            break;
        }
    }
}

It is ugly, it has ugly output, but it does the job :) Try to play around with, for example to achieve nicer output. Change it from switch statement back to if statements, and add function calls if you want. Learn about the above concepts in the tutorials available online, and do not be shy to ask questions if you get stuck. Failure and failure again is OK, that's how you learn - by trying to get better :)