I'm trying to write a program for my programming class using Dev C++ on Windows 7 that displays several arithmetic operations in a table. The program streams data from a text file called 'math.txt' (see below). The character before the numbers in the .txt file is supposed to tell the program which function to call using a switch statement, and if the character doesn't match any of the characters in the switch statement, then it shows up as "invalid operation" in the data table. I've completed the program and it compiles, but for some reason when I run it, instead of showing lines of completed arithmetic operations, the table just displays multiple lines that say "invalid operation" or "1". Can anyone please take a look at my code and explain why this might be happening, and why my switch statement isn't properly calling my functions? Also, please excuse the length of my program, I put a lot of comments in my code because it is required for the assignment.
/******************************************************
******************************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <math.h>
usingnamespace std;
//Prototypes
void doAddition( ifstream & );
void doSubtraction( ifstream & );
void doMultiplicaton( ifstream & );
void doDivision( ifstream & );
void doExponent( ifstream & );
void doFactorial( ifstream & );
int main()
{
//input file
ifstream inFile;
char ch;
inFile.open ( "math.txt" );
if (inFile.fail() )
{
cout<< "The math.txt input file failed to open";
exit(-1);
}
//Read first operation type from the input file
cout<< "Arithmetic Operations";
cout<<endl<<endl;
cout<<"Operation"<<" "<<"Number 1"<<" "<<"Number2"<<" "<<"Result"<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
inFile>> ch;
while(inFile)
{
//switch statement calling the various functions based on the operator contained in the ch variable
switch( ch )
{
case'+': cout << doAddition << endl;
break;
case'-': cout << doSubtraction << endl;
break;
case'*': cout << doMultiplicaton << endl;
break;
case'^': cout << doExponent << endl;
break;
case'/': cout << doDivision << endl;
break;
case'!': cout << doFactorial << endl;
break;
default: cout << "Invalid operation"
<< inFile.ignore( 100, '\n' ) << endl;
}
//Get the next operation type from the input file
inFile>> ch;
}
inFile.close();
return 0;
}
//code functions below this line
/***************************************************************
Function: void doAddition( ifstream &inFile )
Use: to add 2 numbers together if they are preceded by a
+ character in the data file.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doAddition( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1 >> num2;
cout<< "Addition "
<<num1<<" "<<num2<<" "<<"Sum"<<" "
<<num1+num2<< endl;
}
/***************************************************************
Function: Subtract
Use: to subtract one number from the other if they are preceded by a
- character in the data file.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doSubtraction( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;
cout<< "Subtraction "
<<num1<<" "<<num2<<" "<<"Difference"<<" "
<<num1-num2 <<endl;
}
/***************************************************************
Function: Multiply
Use: to multiply 2 numbers together if they are preceded by a
* character in the data file.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doMultiplicaton( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;
cout<< "Multiplication "<<num1<<" "<<num2<<" "<<"Product"<<" "
<<num1*num2 <<endl;
}
/***************************************************************
Function: Divide
Use: to divide 2 numbers if they are preceded by a
/ character in the data file.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doDivision( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;
cout<< "Division "<<num1<<" "<<num2<<" "<<"Quotient"<<" "
<<num1/num2<< "Remain "<< num1%num2 <<endl;
}
/***************************************************************
Function: Exponent
Use: to raise the first number in the data file to the
power of the second number if they are preceded by a
^ character in the data file.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doExponent( ifstream &inFile )
{
int num1;
int num2;
inFile>> num1>> num2;
cout<< "Exponent "<<num1<<" "<<num2<<" "<<"Product"<<"
<< pow(num1, num2) <<endl;
}
/***************************************************************
Function: Factorial
Use: to find the factorial of a number in the data file if
it is preceded by an !.
Arguments: a reference to an input file stream
Returns: None
***************************************************************/
void doFactorial( ifstream &inFile )
{
int num,factorial=1;
inFile>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial "<< num <<""<<"Product "
<<factorial<<endl;
}
I know you might be a beginner, but that code is absolutely atrocious...
Please use indentation that can be easily read. Also, for the love of god, please have the decency to multi-line long couts...
I'm also going to add: delete Dev C++, please, and use somthing that is not horribly outdated. You have a plethora of IDEs to choose from, please pick one (I'm not going to save you the trouble of googling it yourself until you save me the trouble of deciphering your terribly formated code).
My apologies. I edited the code a bit in my previous post. Is that any better? I multi-lined some of the cout statements. I'm not too sure how everything else is supposed to be formatted. Also for the indentation my professor told our class to only indent loops. As for the compiler, my professor suggested either Dev C++ or Quincy, but recommended Dev C++ since that's the one she uses in while teaching the class.
i think iwishiknew is being a bit melodramatic here jnegoda. just because he cant read your formatting doesnt mean its unreadable. i can read it just fine.
I would not expect anyone to read your code that way.
i reiterate, thats a very small view. there is nothing wrong with it and its very bad if you can only read code written in your style, and not other valid ones.
jnegoda: to repeat, there is nothing wrong with your style, if its comfortable for you keep it.
Ok! I'm really new to programming so I didn't know if there was a specific way it was supposed to be formatted or not. But if my style of coding is fine then I'll just keep coding the way I have been.