Program Help

I am trying to make a program that reads in a text file with a math operation on each line. It will then calculate the answer and display the numbers with the result on the screen. When I execute, the code just goes on forever repeating addition(thats the math operation on the first line) and 0's for the numbers and answer(even though that shoudlnt be the case). Can someone help me where I went wrong? An eplaination for why would be greatly appreciated too.
Thanks in advance

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

using namespace std;

int num1, num2, tempNum, result, leftOver;

//These are the function prototypes
void doAddition(int, int);
void doSubtraction(int, int);
void doDivision(int, int);
void doMultiplication(int, int);
void doExponent (int, int);
void doFactorial (int);

int main()
{
// The next 8 lines tell the program to open up a text file and
//read each line
ifstream inFile;

char ch;

inFile.open("math.txt");
if(inFile.fail())
{
cout << "The math.txt input file failed to open";
return 0;
}

inFile >> ch;

//This while loop tells the program to keep the file open if there is another
// character to be read
while(inFile)
{
//This branches off into different math operations that will be done
//depending on what character is read
switch(ch)
{
case '+':doAddition(num1, num2);
break;
case '-':doSubtraction(num1, num2);
break;
case '*':doMultiplication(num1, num2);
break;
case '^':doExponent(num1, num2);
break;
case '/':doDivision(num1, num2);
break;
case '!':doFactorial(num1);
break;
default:inFile.ignore(100, '\n' );
break;

}
}
//closes the file
inFile.close();
}

/******************************************************************

Function: doAddition

Use: Adds num1 and num2

Arguments: the num1 and num2 read in from the file

Returns: The sum

******************************************************************/
void doAddition(int num1, int num2)
{
result=num1+num2;
cout << "Addition\t" << num1 <<"\t" << num2 <<"\tsum:" << result;
cout << endl;
}

/******************************************************************

Function: doSubtraction

Use: Subtracts num1 and num2

Arguments: the num1 and num2 read in from the file

Returns: The difference of num1 and num2

******************************************************************/
void doSubtraction(int num1, int num2)
{
result=num1-num2;
cout<< "Subtraction\t" << num1<<"\t" << num2 <<"\tdifference:" << result;
cout << endl;
}

/******************************************************************

Function: doMultiplication

Use: multiplys num1 and num2

Arguments: the num1 and num2 read in from the file

Returns: The sum

******************************************************************/
void doMultiplication(int num1, int num2)
{
result=num1*num2;
cout << "Multiplication\t"<< num1<<"\t" << num2 <<"\tproduct:" << result;
cout << endl;
}

/******************************************************************

Function: doExponent

Use: Raises num1 to num2

Arguments: the num1 and num2 read in from the file

Returns: The product of the exponent

******************************************************************/
void doExponent(int num1, int num2)
{
result=powl(num1,num2);
cout << "Exponent\t" << num1 <<"\t" << num2 <<"\tproduct:" << result;
cout << endl;
}

/******************************************************************

Function: doDivision

Use: divides num1 from num2

Arguments: the num1 and num2 read in from the file

Returns: The quotient and the remainder

******************************************************************/
void doDivision(int num1, int num2)
{
result=num1/num2;
leftOver=num1%num2;
cout << "Division\t" << num1 <<"\t" << num2 <<"\tQuo:" << result;
cout << "Remain:" << leftOver;
cout << endl;
}

/******************************************************************

Function: doFactorial

Use: Takes the factorial of num1

Arguments: the num1 read in from the file

Returns: The product of the factorial

******************************************************************/
void doFactorial (int num1)
{
result = 1;
for(tempNum=num1;tempNum >= 1; tempNum-- )
result = result * tempNum;

cout << "Factorial\t" << num1 <<"\t\t\t\tproduct:" << result << endl;
}

Here is the text file
+ 23 34
- 9 8
+ 100 1
* 8 7
^ 2 5
/ 45 8
! 4
a 12 -9
! 0
^ 7 0
* -9 2
- -50 324
* -7 -3
/ 10 2
/ 1 2
+ 240 360
! 5
S -999 -1
f 8
^ 16 3
/ 9 5
I can only see one place where the infile is accessed. Immediately after opening, there is this: inFile >> ch;
But none of the numbers are ever read.

Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
    while (inFile >> ch)
    {
        switch(ch)
        {
            case '+':
                inFile >> num1 >> num2;
                doAddition(num1, num2);
                break;

            default:inFile.ignore(100, '\n' );
                break;
        }
    }


One other point. There are a lot of global variables:
int num1, num2, tempNum, result, leftOver;
These should instead be defined locally within whichever function will use them. For example num1 and num2 should be defined inside function main()
Topic archived. No new replies allowed.