Infile Calculator?

I'm trying to read in a set of data from an infile and have my calculator read the numbers and output sums, running into a bit of trouble though.

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <fstream>

using namespace std;


class Calculator
{
public:
void candoit()
{
float numb1;
float numb2;
char sign;
float sum;

ifstream numbfile;
numbfile.open("infile.txt");
numbfile>>numb1>>sign>>numb2;

cout << "CALCULATOR PROGRAM" << endl;



while( cin >> numb1 >> sign >> numb2 ) // If nnumb1 of the numb2o integers equal to EOF, enter the loop.
{
// Determine the sum of the operatinumb1.
switch( sign ) // switch loop for the char.
{
case '+':
sum = numb1 + numb2;
cout << numb1 << sign << numb2 << " = " << endl << sum << endl << endl;
break; // exit switch loop

case '-':
sum = numb1 - numb2;
cout << numb1 << sign << numb2 << " = " << endl << sum << endl << endl;
break; // exit swtich loop

case '*':
sum = numb1 * numb2;
cout << numb1 << sign << numb2 << " = " << endl << sum << endl << endl;
break; // exit switch loop

case '/':
sum = numb1 / numb2;
cout << numb1 << sign << numb2 << " = " << endl << sum << endl << endl;
break; // exit switch loop

default: // To catch all other characters.
cout << "\nBad Operator. '" << sign << "'" << endl;
cout << "Please enter a new operator or EOF to exit\n";
break; // exit switch loop

} // end switch loop

} // end while loop

} // end functinumb1 candoit

}; // end class calculator


int main()
{

Calculator doit; // Creating an object from the class Calculator.


doit.candoit(); // call the functinumb1 *candoit* from the object created *doit* under the class name Calculator.


return 0; // To indicate successfully terminatinumb1.

}

Why are we using this class as a function? That's not how you do this my friend. Please see here: http://www.cplusplus.com/doc/tutorial/classes/

Thanks for the reply

I'm starting to realize that I'm completely lost. These were my instructions.

Your program should use a end of file loop to read the two floats (operands) and a character operator ( + , - , * , / ).

For each line of input your main function should call one of four(4) value-returning functions (e.g. add, subtr, mult, divide) each designed to perform the indicated operation on the two floats. Each function performs the appropriate operation and returns the result to the main function.

The main function must display the operands, the operator and the result.
Ok, so first things first this isn't Java so functions do not have to be tied to classes that simplifies things a little for you.

To keep it simple we'll use your main function to do the reading and storing of the characters. So an end of file loop looks some thing like this: while(!InputFile.eof()){...} so let's start by putting that into your code.

EDIT: Also delete this Class that you have going, your instructions didn't ask for it and we won't be needing it. You can start a new thread to ask about classes if you're still interested later.
Last edited on
Topic archived. No new replies allowed.