I am getting the following errors
25 D:\calculat.cpp expected `(' before "op"
49 D:\calculat.cpp expected `;' before "op"
53 D:\calculat.cpp `y' undeclared (first use this function)
I partly suspect to change it to switch (op) but that gives more errors of the following
27 D:\calculat.cpp expected primary-expression before ':' token
28 D:\calculat.cpp no match for 'operator=' in 'frac3 = CFract::operator+(CFract&)(((CFract&)(&frac2)))'
in each case statement

#include <iostream>
#include <iomanip>
#include <ctype.h>
#include "fraction.h"

using namespace std;

int main()
{
    CFract frac1;
    CFract frac2;
    CFract frac3;
    char choice;
    char op;
    int err;
    err = 0;
    
    do
    {
             cout << "Enter a fraction (a/b), operator(+ - * /), and fraction (c/d),"
             "e.g. 1/2 - 1/3: " << endl;
             cin >> frac1 >> op >> frac2;
             
             switch (op)
             {
                    case +:
                         frac3 = frac1 + frac2;
                         break;
                    case -:
                         frac3 = frac1 - frac2;
                         break;
                    case *:
                         frac3 = frac1 * frac2;
                         break;
                    case /:
                         frac3 = frac1 / frac2;
                         break;
                    else
                        err = 1;
             }
             if(err = 1)
             {
                    cerr << "*** Illegal operator" << endl;
                    err = 0;
             }
             else
             {
                    cout << "The result of " << frac1 op frac2 << " is " << frac3 << endl;
             }
             cout << "Try another (y,n)? " << endl;
             cin >> choice;
    }while (tolower(choice) = y);
    return 0;
}

whet testing single chars you need to use the single quote such as

if (op == '+')   // this is okay

if (op == +)  // this is not okay

also when you are testing conditions you are using a single =. testing equality in c++ is done using ==

if (foo == boo)  // testing if foo equals boo

if (foo = boo)  //  makes foo equal to boo

great got everything fixed except for this

cout << "Try another (y,n)? " << endl;             
cin >> choice;
}while (tolower(choice) == y);

which is saying y is undeclared. The objective is if the user inputs y to do the loop again. Do I need to declare y somewhere or do I have the syntax wrong?

This article has been dead for over six months. Start a new discussion instead.