Here is the website for the assignment I am working on:

Here is the website for my header file (MixedExpression.h):

Here is the website for my library source file (MixedExpression.cpp):

Here is the website for my calculator client file (Calculator.cpp):

My question: I got the code in the header file correct. In the library source file, I got the default constructor, GCD(), and printData() functions correct. I am not sure if I got the reduce() and ReadMixedExp() functions correct. I am mostly having trouble with the normal constructor because my teacher wants me to reduce the expression in that which I can't figure out. I am also not sure if I am doing the calculations correctly. In the calculator client file, I don't know if I am using the switch statement correctly. Right now, all I am getting is this output: (0 + 0/1), which is going on in an infinite loop. What do I need to fix, add, or change?

Recommended Answers

All 3 Replies

Last file. Yuo declare char symbol you use it in the switch statement, but I never see it getting any value.

commented: Yep +9

Here is my updated source code for the calculator file if this is better:

#include "MixedExpression.h"
using namespace std;

int main(int argc, char *argv[])
    MixedExpression res;
    MixedExpression op1;
    MixedExpression op2;
    char symbol;
    long x,y;
    ifstream in;
    ofstream out;

    if(argc > 1)
                if(symbol == '+')
                    res = op1.add(op2); // Get the sum.
                else if(symbol == '-')
                    res = op1.subtract(op2); // Get the difference.
                else if(symbol == '*')
                    res = op1.multiply(op2); // Get the product.
                else if(symbol == '/')
                    res = op1.divide(op2); // Get the quotient.
        // Close the input file.
        // Close the output file.
    return 0;

I got rid of the switch statement and used if-elseif statements, but I am still getting the infinite loop. Is there something else that needs to be changed?

Believe you misunderstood Ddanbe's point, I am afraid. The issue he was pointing out was not the use of the switch() statement; that was fine. The problem is that the variable you are testing, symbol, is never getting set to a usable value. You need to read in symbol after reading in the op1 (not res, as yo currently have it, BTW). So, going back to your original code, it would look like this:

        in >> symbol;

        { //.. etc.
commented: Yep +9
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.