Very simple calculator

Started by
21 comments, last by foxefde 10 years, 11 months ago

Okay, the objective of this is to have the user pick one of the four basic arithmetic operations (addition, subtraction, multiplication and division), and then pick the 2 integers for the operation, and then have the program spit out the result. I seem to be having a problem storing the symbol for the math operation and then using it at the end to actually perform the math. Here's what I got so far:


#include <iostream>

using namespace std;

int main ()
{
    char symbol;
    int a;
    int b;

    cout << "Please decide what kind of operation you want done; +, -, * or /: ";
    cin >> symbol;

    cout << "Now please enter the first number: ";
    cin >> a;

    cout << "And the second: ";
    cin >> b;
    int c = a, symbol, c;

    cout << c;
}

I think my problem is how the symbol is stored, and char isn't cutting it.

So, any advice would be great!

Advertisement
Char will work fine, your misconception is about operators and variables. The statement "int c = a, symbol, c" actually tries to declare three variables, "c" with the initial values of "a", "symbol" (which already exists) and "c" again.

In any case, which operator is used is fixed at compile time, you cannot use the value of a variable to change it. What you can do is introduce conditional code paths where the operator used is different, e.g. using an if or switch statement.

int c = a, symbol, c

won't work. EDIT: And you meant to use b at the end anyway ;)

You need to do a switch statement (or some if statements) depending on the value of symbol. Something like

EDIT2: And check out my funky drop through case statements that allows *, x and X for multiplication!


switch(symbol)
{
case '+': c = a + b; break;
case '*':
case 'x':
case 'X': c = a * b; break;
case '-': c = a - b; break;
case '/': c = a / b; break;
default: // handle errors here
}
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Okay, so, I got rid of trying to define c and implemented a 4 part else if statement (I haven't learned about switch yet), and now I'm getting one last error:


#include <iostream>
#include <string>

using namespace std;

int main ()
{
    string symbol;
    int a;
    int b;

    cout << "Please decide what kind of operation you'd like to complete: ";
    cin >> symbol;

    cout << "And now please decide the first number for the operation: ";
    cin >> a;

    cout << "And now the second, please: ";
    cin >> b;

    if ( symbol == "+" )
    {
        cout << a << " + " << b << " = " << a + b << endl;
    }
    else if ( symbol == "-" )
    {
        cout << a << " - " << b << " = " << a - b << endl;
    }
    else if ( symbol == "*" )
    {
        cout << a << " * " << b << " = " << a * b << endl;
    }
    else
    {
        cout << a << " / " << b << " = " a / b << endl; // error: expected ';' before 'a'
        return 0;
    }
}

You are missing an << between " = " and a / b.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

You are missing an << between " = " and a / b.

Haha, thank you!

Did you noticed that right away Paradigm, or did the error give you an idea as to what to look for? Because whenever I look at the compiler error, all I ever manage to understand in it is "error: this crap is broke!"

Okay, so, I got rid of trying to define c and implemented a 4 part else if statement (I haven't learned about switch yet), and now I'm getting one last error:



cout << a << " / " << b << " = " a / b << endl; // error: expected ';' before 'a'

Because the << operator allows you to output one value, and you're trying to output a string followed by a number. What you want is:


cout << a << " / " << b << " = " << a / b << endl;

You are missing an << between " = " and a / b.

Haha, thank you!

Did you noticed that right away Paradigm, or did the error give you an idea as to what to look for? Because whenever I look at the compiler error, all I ever manage to understand in it is "error: this crap is broke!"

Wait until you get errors relating to templates ;) Fun fun fun.

You get better at deciphering the messages with experience.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Did you noticed that right away Paradigm, or did the error give you an idea as to what to look for? Because whenever I look at the compiler error, all I ever manage to understand in it is "error: this crap is broke!"

These "error: expected ';' before..." messages can be pretty cryptic, but in this case it's actually fairly helpful. Basically it just means that the parser got confused somewhere in that line, in this case right before the 'a'. So look at the line and find the first 'a', and if there's nothing wrong right before it keep looking for another 'a'. In this case where that second 'a' is should be either a ';' or another '<<' or whatever else that makes sense.

But generally this error just means look at that line until you find what's wrong, and sometimes that means the error is actually in the previous line.

These "error: expected ';' before..." messages can be pretty cryptic, but in this case it's actually fairly helpful. Basically it just means that the parser got confused somewhere in that line, in this case right before the 'a'. So look at the line and find the first 'a', and if there's nothing wrong right before it keep looking for another 'a'. In this case where that second 'a' is should be either a ';' or another '<<' or whatever else that makes sense.

But generally this error just means look at that line until you find what's wrong, and sometimes that means the error is actually in the previous line.

Thank you 0r0d. I think understanding how to debug your own code is one of the most important parts of programming, and I'd definitely like to understand how the computer see's these errors.

This topic is closed to new replies.

Advertisement