Nov 15, 2013 at 6:32am UTC
Mr program is supposed to read a series of functions from an input file.
Ex.
4 + 4
34 / 12.3
etc.
Then my program needs to echo the function to an output file and produce a result. My function for calculating should be fine but I don't know how to read the input file and then echo. If you can help please respond. Thanks.
What I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ofstream fout;
ifstream fin;
void getName(string &firstname, string &lastname);
string getLectureSection(string &secnum);
void printIdInfo(string firstname, string lastname, const string CLASS, const string DUE_DATE, string secnum);
void readExpression(double operand1, char operatr, double operand2);
void echoExpression(double operand1, char operatr, double operand2);
void evaluateExpression(double operand1, char operatr, double operand2);
void writeFileLocation();
int main ()
{
string firstname,
lastname,
secnum;
fin.open ("prog5_001in.txt");
fout.open ("prog5_001out.txt");
double operand1,
operand2;
const string CLASS = "C.S.1428.",
DUE_DATE = "11/15/2013";
char operatr;
getName(firstname, lastname);
getLectureSection(secnum);
printIdInfo(firstname, lastname, CLASS, DUE_DATE, secnum);
writeFileLocation();
}
void getName(string &firstname, string &lastname)
{
cout << "Please enter your first name: ";
cin >> firstname;
cout << "Please enter your last name: ";
cin >> lastname;
}
string getLectureSection(string &secnum)
{
cout << "Please enter your three digit section number: ";
cin >> secnum;
cout << endl << endl;
return secnum;
}
void printIdInfo(string firstname, string lastname, const string CLASS, const string DUE_DATE, string secnum)
{
cout << firstname << " " << lastname << endl
<< CLASS << secnum << endl
<< DUE_DATE << endl << endl;
fout << firstname << " " << lastname << endl
<< CLASS << secnum << endl
<< DUE_DATE << endl << endl;
}
void readExpression(double operand1, char operatr, double operand2)
{
fin >> operand1 >> operatr >> operand2;
}
void echoExpression(double operand1, char operatr, double operand2)
{
fout << operand1 << " " << operatr << " " << operand2;
}
void evaluateExpression(double operand1, char operatr, double operand2)
{
switch (operatr)
{
case '+' :
fout << " = " << operand1 + operand2 << endl;
break;
case '-' :
fout << " = " << operand1 - operand2 << endl;
break;
case '*' :
fout << " = " << operand1 * operand2 << endl;
break;
case '/' :
if (operand2 == 0)
{
fout << " Division by zero produces an undefined result." << endl;
}
else
{
fout << " / " << operand1 + operand2 << endl;
}
break;
default :
fout << " Encountered unknown operator." << endl;
break;
}
}
void writeFileLocation()
{
cout << endl
<< "Program results have been written to prog5_001out.txt."
<< endl;
}