problem with output display

The program reads value from a file and calculates the future value and then displays them. If any value is negative or zero it says the value has to be greater than zero. So im having two problems. 1) when it does calculator the future value it does print every value correctly but prints them twice. 2) when the values are zero or less and it prints the error message it should not go to the next line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#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*/

unsigned int read(ifstream &, double &, double &, int &);  // to read from a file

														   /*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 (!inputFile.eof()) {

				int check = read(inputFile, 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;

}

unsigned int read(ifstream &file, double & presentValue, double &interestRate, int &months) {


	if (file.eof())   // reached end of file

		return 0;


	file >> presentValue;  // read present value

	file >> interestRate;  // read interst rate

	file >> months;  // read number of months

	return isValid(presentValue, interestRate, months);

}
Hello fj2200,

Without actually testing your code the first thing I see is with line 62. Using a while condition based on "eof" does not work the way you are thinking. By the time while detects "eof" it will have processed the last goof read twice.

If you want to use "eof" in the while condition the first read will need to be outside the while loop and the next read will have to be at the end of the while loop.

I also see the same potential problem with line 121.

Usually some or all of reading a file is done in the while condition, so if the read fails the while condition will fail.

I will need to test the program to see if the read function will work in the while condition. After that I will be better able to see how the rest of the program works.

I could use a sample of your input file to know what I have to work with.

Hope that helps,

Andy
Thank you for helping me. one of the input file has values 100 .99 36. the future value will be 142.57 so the output would have values in this order 142.57, 100.00, .99, 36
Hello fj2200,

This will be parts of your code, but you should get the idea.

In main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// <--- Added "trunc" for testing. Remove or change "trunc" to "app" when finished.
ofstream outputFile("output.xls", std::ios::trunc);


while (check = read(inputFile, presentValue, interestRate, months))
{
	if (check == 1)
	{

		double futureValue = calculateFutureValue(presentValue, interestRate, months);

		// Changed the output so it would look better.
		outputFile << std::setprecision(2) << std::fixed << std::setw(8) << futureValue << std::setw(17) << presentValue << std::setw(17) << interestRate << std::setw(14) << months << endl;
		//std::cout << std::endl;  // Used in debug mode for testing.
	}


The read function:
1
2
3
4
5
6
7
8
9
10
11
12
13
unsigned int read(ifstream &file, double & presentValue, double &interestRate, int &months)
{
	file >> presentValue;  // read present value

	file >> interestRate;  // read interst rate

	file >> months;  // read number of months

	if (file.eof())   // reached end of file  // <--- Moved down. Works better here.
		return 0;

	return isValid(presentValue, interestRate, months);
}


Turned out not to be as bad as I first thought. The changes were minor. Still need to make up some more numbers for testing, but I think it should work.

Hope that helps,

Andy
so i changed my code but it keeps saying infinite loop. i thin the error is in the line "while (check = read(inF, presentValue, interestRate, months))"

#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*/

unsigned int read(ifstream &, double &, double &, int &); // to read from a file

/*prototype for reading input values from file*/


int main() {

string filename;

double presentValue; // variable for present valur

double interestRate; // value for interest rate

int months;


cin >> filename; //input file


ifstream inF(filename);


if (!inF.is_open()) {

cout << "File \"" << filename << "\" could not be opened" << endl; // if error with file

}

else {


ofstream of("output.xls");

if (!of.is_open()) {

cout << "File output.xls could not be opened " << endl;

}

else {


of << "Future Value\tPresent Value\tMonthly Interest\tMonths" << endl; //output statement
while (!inF.eof()) {
int check = read(inF, presentValue, interestRate, months);
while (check = read(inF, presentValue, interestRate, months))
{


if (check == 1)
{

double futureValue = calculateFutureValue(presentValue, interestRate, months);


of << std::setprecision(2) << std::fixed << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << endl;

}

if (check == 2) {

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

return futureValue;

}


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





inF >> presentValue; // read present value

inF >> interestRate; // read interst rate

inF >> months; // read number of months

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

{

return 2;

}

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

return 1;}

else

{

while (inF.eof()) {

return 0;

}


}
}
for input1
-10000 1.1 48
10000 -1.0 12
10000 1.1 0
0 0 0

output should be

-10000.00 1.10 48
One or more of the above values are not greater than zero
10000.00 -1.00 12
One or more of the above values are not greater than zero
10000.00 1.10 0
One or more of the above values are not greater than zero
0.00 0.00 0
One or more of the above values are not greater than zero

for input 2
100 .99 36
output should be

Future Value Present Value Monthly Interest Months
142.57 100.00 0.99 36

It's not a good idea to test for eof() in a loop condition. It can cause unexpected behaviour - including infinite loops, or processing the last value more than once.

Here's a recommendation. Don't use eof at all. Keep the read function separate from the validation of values. Just let each function do a single task, and do it well.

Function prototype:
 
bool read(ifstream &, double &, double &, int &); 


The function implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
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();
}


And the corresponding code inside main()

1
2
3
4
5
6
7
8
9
    // Loop while reading from file is successful

    while ( read(inputFile, presentValue, interestRate, months) )
    {
        int check = isValid(presentValue, interestRate, months);
                
        // here, place the code to test the value of check
        // and take the required actions
    }



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

First, I'm sorry that my suggested code does not meet the requirements of the assignment. On reading the question, it gives very specific instructions on function names, return values and so on, and I've moved away from that. If this is part of a formal study course you may need to comply more strictly with the requirements in order to pass.

On this particular question of incorrect results, I think it is that the code is (according to the instructions) supposed to be written in a certain way. In particular, it says this:

The_Question wrote:
Note that the monthly interest rate will be a number such as 10 or 12.5. These are to be read in as percentages (10% and 12.5%). You will need to divide these values by 100 to convert them into the values needed in the function (.1 and .125 for the above values). You need to do this conversion before you call the calculateFutureValue function (see below).


What that means is that the calculation in your calculateFutureValue function:

 
    futureValue = presentValue * (pow((1 + (interestRate / 100)), months));  // future value calculation 

should probably look more like this
 
    futureValue = presentValue * pow(1 + interestRate, months);  // future value calculation 

... and that means that at some point between the time you read the value from the file, and calling the function, you'd need to divide the interest rate by 100.
Hello fj2200,


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


Check the percentage rate. What the above shows is that the percentage rate has been divided by 100 and then your program divides this number again. Thus the incorrect answer. When I changed this to "0.08" and "0.25" it worked fine.

Still looking at if you followed the directions properly, but so far it looks OK.

Hope that helps,

Andy
Topic archived. No new replies allowed.