infile homework help.

Hello,

I have a program for class that performs various arithmetic operations including: addition, subtraction, multiplication, division, exponents, and factorials. I have to use an infile with a switch statement that calls the functions to perform the operations. I have no idea how to do the functions part. Also every time I try to run my program, the file doesn't open. I'm not sure how the rest of my program looks, but all help is appreciated, and half of it was given from my teacher for us to use. I only need this class for my major, I'm not going into computer science so this is really difficult for me to grasp.



#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
int main ()
{
ifstream inFile;

char ch;
void doAddition (ifstream &inFile);
void doSubtraction (ifstream &inFile);
void doMultiplication (ifstream &inFile);
void doDivision (ifstream &inFile);
void doExponent (ifstream &inFile);
void doFactorial (ifstream &inFile);

inFile.open( "math.txt" );
if( inFile.fail() )
{
cout<<"The math.txt input file failed to open";
exit(-1);
}
//Read the first operation type from the input file

inFile >> ch;

//While there are mathematical operations to be performed

while ( inFile )
{
switch (ch)
{
case '+':
void doAddition(ifstream &inFile);
break;
case '-':
void doSubtraction(ifstream &inFile);
break;
case '*':
void doMultiplication(ifstream &inFile);
break;
case '/':
void doDivision(ifstream &inFile);
break;
case '^':
void doExponent(ifstream &inFile);
break;
case '!':
void doFactorial(ifstream &inFile);
break;
default: "\nInvalid Operation";
inFile.ignore(100, '\n');
break;
}
} //switch statement that will call various functions based on the
//operator contained in the ch variable


//Get the next operation type from the input file
inFile >> ch;
inFile.close ();
return 0;
}

/***************************************************************/
void doAdditon (ifstream &inFile)
{

}
/***************************************************************
Function: void doAddition (ifstream &inFile)

Use: This will calculate and display a sum.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/

void doSubtraction (ifstream &inFile)
{

}
/***************************************************************
Function: void doSubtraction (ifstream &inFile)

Use: This will calculate and display the difference of two numbers.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/

void doMultiplication (ifstream &inFile)
{

}
/***************************************************************
Function: void doMultiplication (ifstream &inFile)

Use: This will calculate and display the product of two numbers.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/

void doDivision (ifstream &inFile)
{

}
/***************************************************************
Function: void doDivision (ifstream &inFile)

Use: This will calculate and display the quotient and remainder of two numbers.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/
void doExponent (ifstream &inFile)
{

}
/***************************************************************
Function: void doExponent (ifstream &inFile)

Use: This will calculate and display the product of raising a number to a power.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/
void doFactorial (ifstream &inFile)
{

}
/***************************************************************
Function: void doFactorial (ifstream &inFile)

Use: This will calculate and display the factorial of a number.

Arguments: A reference to an input file stream.

Returns: None
***************************************************************/


The output should look like this but more lined up obviously:

Arithmatic Operations

Operation Number 1 Number 2 Result
----------------------------------------------------------------------------

Addition 23 34 Sum: 57

Subtraction 9 8 Difference: 1

Addition 100 1 Sum: 101

Multiplication 8 7 Product: 56

Exponent 2 5 Product: 32

Division 45 8 Quo: 5 Remain: 5

Factorial 4 Product: 24

Invalid operation

Factorial 0 Product: 1

Exponent 7 0 Product: 1

Multiplication -9 2 Product: -18

Subtraction -50 324 Difference: -374

Multiplication -7 -3 Product: 21

Division 10 2 Quo: 5 Remain: 0

Division 1 2 Quo: 0 Remain: 1

Addition 240 360 Sum: 600

Factorial 5 Product: 120

Invalid operation

Invalid operation

Exponent 16 3 Product: 4096

Division 9 5 Quo: 1 Remain: 4
Last edited on
Topic archived. No new replies allowed.