thanks or your suggestion, but can you help with one more question. so my code works correctly for most of the input except for two. It says it didn't calculate correctly even though its right. ill write down what it displays.
The call to calculateFutureValue(100.00, 0.0008, 36) returned an unexpected value of 100.03
The call to calculateFutureValue(12345678901234.00, 0.0025, 60) returned an unexpected value of 12364211083596.52
Here is the link to the question
http://www.chegg.com/homework-help/questions-and-answers/lab-lesson-9-two-parts-part-2-making-use-functions-pass-reference-files-part-2-worth-65-po-q25222741
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>
using namespace std;
double calculateFutureValue(double, double, int); // to calculate futer value
/*prototype for reading input values from file*/
bool read(ifstream &, double &, double &, int &);
/*prototype for reading input values from file*/
unsigned int isValid(double, double, int); // for values thar are invalid
int main() {
string filename;
double presentValue; // variable for present valur
double interestRate; // value for interest rate
int months;
cin >> filename; //input file
ifstream inputFile(filename);
if (!inputFile.is_open()) {
cout << "File \"" << filename << "\" could not be opened" << endl; // if error with file
}
else {
ofstream outputFile("output.xls");
if (!outputFile.is_open()) {
cout << "File output.xls could not be opened " << endl;
}
else {
outputFile << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl; //output statement
while ( read(inputFile, presentValue, interestRate, months) )
{
int check = isValid(presentValue, interestRate, months);
if (check == 1) {
double futureValue = calculateFutureValue(presentValue, interestRate, months);
outputFile << std::setprecision(2) << std::fixed << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << endl;
}
else if (presentValue <=0 || interestRate <=0 || months <= 0) {
cout << std::setprecision(2) << std::fixed << presentValue << " " << interestRate << " " << months << endl;
cout << "One or more of the above values are not greater than zero" << endl; // error message for invalid values
}
}
}
}
return 0;
}
double calculateFutureValue(double presentValue, double interestRate, int months) {
double futureValue; //variable for future value
futureValue = presentValue * (pow((1 + (interestRate / 100)), months)); // future value calculation
return futureValue;
}
unsigned int isValid(double presentValue, double interestRate, int months) {
if (presentValue <= 0 || interestRate <= 0 || months <= 0)
return 2;
else
return 1;
}
bool read(ifstream &file, double & presentValue, double &interestRate, int &months)
{
file >> presentValue; // read present value
file >> interestRate; // read interest rate
file >> months; // read number of months
// check status after reading from file
// if all three values were read successfully,
// the file status will be good.
return file.good();
}