Need help with switch function based calculator

Hello! I'm trying to write a program that will simulate a calculator by using switches that call functions to perform the desired task. When I try compiling I get error messages "error: no match for 'operator>>' in 'infile.std::basic_ifstream<char>::<enonymous.std..."

Any help would be appreciated!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212`` ``````#include #include #include using namespace std; int add (ofstream &); int subtract (ofstream &); int multiply (ofstream &); double divide (ofstream &); int remainder (ofstream &); double average (ofstream &); int maximum (ofstream &); int minimum (ofstream &); int square (ofstream &); //ifstream infile ("C:\\Users\\DavidC\\Desktop\\HW_5_INPUT.txt"); ifstream infile ("con"); //ofstream outfile ("C:\\Users\\DavidC\\Desktop\\HW_5_OUTPUT.txt"); ofstream outfile ("con"); int main () { char choice; bool not_done = true; do { cout << "Please select a function to perform." << endl << endl; cout << "Enter\t\t\tFor" << endl; cout << "+\t\t\tAddition\n-\t\t\tSubtraction\n*\t\t\tMultiplication\n/\t\t\tDivision\n%\t\t\tRemainder\nA\t\t\tAverage of two numbers\nX\t\t\tMaximum of two numbers\nM\t\t\tMinimum of two numbers\nS\t\t\tSquare of a number\nQ\t\t\tQuit program"; infile >> choice; switch (choice) { case '+': add (outfile); break; case '-': subtract (outfile); break; case '*': multiply (outfile); break; case '/': divide (outfile); break; case '%': remainder (outfile); break; case 'A': average (outfile); break; case 'X': maximum (outfile); break; case 'M': minimum (outfile); break; case 'S': square (outfile); break; case 'Q': not_done = false; default: outfile << "Error: Please enter a valid selection." << endl; break; } } while (not_done); return 0; } int add (ofstream &out) { int x, y, sum; out << "Please enter the addend: "; infile >> x >> endl; out << "Please enter the augend: "; infile >> y >> endl; sum = x + y; return sum; } int subtract (ofstream &out) { int x, y, difference; out << "Please enter the minuend: "; infile >> x >> endl; out << "Please enter the subtrahend: "; infile >> y >> endl; difference = x - y; return difference; } int multiply (ofstream &out) { int x, y, product; out << "Please enter the multiplier: "; infile >> x >> endl; out << "Please enter the multiplicand: "; infile >> y >> endl; product = x * y; return product; } double divide (ofstream &out) { double x, y, quotient; out << "Please enter the dividend: "; infile >> x >> endl; out << "Please enter the divisor: "; infile >> y >> endl; quotient = (double)(x / y); return quotient; } int remainder (ofstream &out) { int x, y, remain; out << "Please enter the dividend: "; infile >> x >> endl; out << "Please enter the divisor: "; infile >> y >> endl; remain = x % y; return remain; } int average (ofstream &out) { int x, y, avg; out << "Please enter the first number: "; infile >> x >> endl; out << "Please enter the second number: "; infile >> y >> endl; avg = (x + y)/2; return avg; } int maximum (ofstream &out) { int x, y, max; out << "Please enter the first number: "; infile >> x >> endl; out << "Please enter the second number: "; infile >> y >> endl; if (x > y) max = x; else max = y; return max; } int minimum (ofstream &out) { int x, y, min; out << "Please enter the first number: "; infile >> x >> endl; out << "Please enter the second number: "; infile >> y >> endl; if (x < y) min = x; else min = y; return min; } int square (ofstream &out) { int x, squared; out << " Please enter the number that you would like to square: "; infile >> x >> endl; squared = x * x; return squared; }``````
I don't think you need `endl` on all your input statements.
I would try to simplify you solution significantly.

@Chervil is right as well, the endl is not necessary. You should look at using std::getline() as it's not prone to errors like the >> operator.

Some points of interest for you.
1. You don't need to pass in the ofstream to every method because it's declared out of the main() method scope.
2. Why are you reading and writing everything from a file and not the console? (cin/cout)
3. You don't need to create a variable to hold the results of all of your methods.
e.g return x * x;
is better than create a temp variable to hold the result only to return it on the following line (it's only helpful when using a debugger.. but even then marginally)
 Some points of interest for you. 1. You don't need to pass in the ofstream to every method because it's declared out of the main() method scope.

I agree with Zaita.
I think if you delete these lines:

 ``1234`` ``````//ifstream infile ("C:\\Users\\DavidC\\Desktop\\HW_5_INPUT.txt"); ifstream infile ("con"); //ofstream outfile ("C:\\Users\\DavidC\\Desktop\\HW_5_OUTPUT.txt"); ofstream outfile ("con");``````

and add these lines on the begining of main() :

 ``12`` ``````ifstream infile.open("con.txt"); ofstream outfile.open("con.txt");``````

you will not get this error. Because you missed opening file and did not call open functions anywhere, so your ifstream types are empty now.
Another tip. Make the calculator a resusable static class.
There isn't much gained by having it as a static class. Putting the methods in a class and holding an instance of it in the main method would more than suffice.
It makes it easier to use. A simple calculator doesn't need an instance since you are merely using single commands to output and answer. It is basically a utility class.

If it was a graphing calculator then an instance would make more sense but even then, when are you ever going to create multiple instances of the calculator?
Last edited on
But the same argument would be applied to putting them into a class. Why? There is nothing gained from having them as static methods on a class.

If you want to isolate the code for a better implementation then put the functions into a namespace, but there is no reason to go OO for the sake of going OO.
Topic archived. No new replies allowed.