HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Four-function and exponentiation console calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fourfunctionandconsolecalculatorexponentiation

Problem

This code works as it is supposed to:

#include
#include
#include

using namespace std;

int main()
{
    char cAgain, type;
    int x, y=0;
    double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2), 
        multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;
    do
    {
        cout > x;
        if (x > num1 >> type >> num2;
                switch (type)
                {
                case '+':
                    total = addition(num1, num2);
                    cout > cAgain;
    } while (cAgain == 'Y' || cAgain == 'y');
return 0;
}

double addition(double num1, double num2)
{
    double total;
    total = num1 + num2;
    return (total);
}
double subtraction(double num1, double num2)
{
    double total;
    total = num1 - num2;
    return (total);
}
double division(double num1, double num2)
{
    double total;
    total = num1 / num2;
    return (total);
}
double multiplication(double num1, double num2)
{
    double total;
    total = num1 * num2;
    return (total);
}
double exponential(double num1, double num2)
{
    double total;
    total = pow(num1,num2);
    return (total);
}


It starts by asking for the number of operations to perform. You enter a number, such as 3. Then it asks you to enter in your operation, such as 5+5, which is 10. Then it asks for a second, such as 10^2, which is 100. Then it asks for a third, such as 100-10, which is 90. Then it asks if you'd like to start over.

Solution

I'll mention some additional points that haven't been mentioned yet, but this does look like it can still be cleaned out quite a bit.

-
You could define those functions above main() so that prototypes won't be needed. Otherwise, put them above main(), as already mentioned in another answer.

-
These:

#include
#include
#include


would look nicer with spaces:

#include 
#include 
#include 


Regarding spacing, you have some inconsistent spacing around the cases. Try not to neglect this as it could make your code more awkward to read for others.

-
type, x, and y are all declared outside of the loop, but are only used inside of the loop. In order to help keep track of them, declare them right where they're first used. Doing this with every variable can help with maintaining cleaner code, especially with larger programs.

-
This is not doing what you expect:

while (x != 0, x--)


The comma operator returns the result of the last expression, which is the x--. The other expressions are evaluated (from left to right) but the results of the expression are ignored. Thus, the result of x != 0 is evaluated with no side affects and the result dropped. This makes the expression equivalent to while (x--).

-
You don't check for division by 0, so your program can break if this operation is done. You may throw an exception for that and handle it, so that you can get a different input. Just make sure this division doesn't happen.

-
You don't need return 0 at the end of main(). The compiler will do this return for you after reaching this point.

Code Snippets

#include<iostream>
#include<cmath>
#include<cstdlib>
#include <iostream>
#include <cmath>
#include <cstdlib>
while (x != 0, x--)

Context

StackExchange Code Review Q#74726, answer score: 11

Revisions (0)

No revisions yet.