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
//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;
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()