vendredi 24 novembre 2017

C++ in need to fix output, related to "Making use of functions, pass by reference, and files."

I wrote a program, but when i submit it, I'm facing problems that i don't know how to fix. and that's were i need your help. here is the program:

#include<iostream>

#include<fstream>

#include<cmath>

#include<string>

#include<iomanip>

using namespace std;

/**

* Function to calculate the future value and return the same

*/

double calculateFutureValue(double presentValue, double interestRate, int months)

{

double futureValue = (double)presentValue * pow( (1 + interestRate), months);

return futureValue;

}

/**

* Function to read the input file and assign the value to the variables

*/

unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months)

{

inF >> presentValue;

inF >> interestRate;

inF >> months;

if(presentValue <= 0 || interestRate <= 0 || months <= 0)

{

return 2;

}

else if( !(presentValue <= 0 || interestRate <= 0 || months <= 0))

return 1;

else

{

if(inF.eof())

return 0;

}

}

/**

* Function to write data to file

*/

void write2File(ofstream &of, double futureValue, double presentValue, double interestRate, int months)

{

of << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << "\n";

}

// main function

int main(void)

{

string infile;

cin >> infile;
ifstream inF;

inF.open(infile.c_str());

if(!inF)

{

cout << "File "<< "\"" << infile << "\"" <<" could not be opened" <<endl;

return -1;

}

string outfile = "output.xls";

ofstream of;

of.open(outfile.c_str());

if(!of)

{

cout << "Error in creating the output file" << endl;

return -1;

}

of << "Future Value\tPresent Value\tMonthly Interest\tMonths\n"; // printing     header to output file

double futureValue;

double presentValue;

double interestRate;

int months;

of << fixed << setprecision(2); // to print 2 places after decimal into file

while(!inF.eof()) // looping untill end of file

{

int returnVal = readfile(inF, presentValue, interestRate, months);

if(returnVal == 2)

{

cout << presentValue << "\t" << interestRate << "\t" << months << endl;
cout << "One or more of the above values are not greater than zero." << endl;

}

else if(returnVal == 1)

{

futureValue = calculateFutureValue(presentValue, interestRate/100, months);

write2File(of, futureValue, presentValue, interestRate, months);

}

}

inF.close();

of.close();

return 1;

}

Here is a link for two pictures of my output and expected output.

http://ift.tt/2jkofZn

For some reason my program outputs extra things that i dont need it to.

Thanks in advance for your help.

Aucun commentaire:

Enregistrer un commentaire