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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
#ifndef AMORTIZATION_H
#define AMORTIZATION_H
class Amortization
{
private:
double P; // principle
double I; // interest
double N; // periods
public:
Amortization(); //default constructor (prototype)
Amortization(double p, double i, double n);
double getPayment(); // Looking at the calls to getPayment, it appears that it takes no parameters.
// also you must indicate what type it returns (a double)
};
#endif // ! AMORTIZATION_H
// [Amortization.cpp]
#include <iostream>
#include <cmath>
// #include "Amortization.h"
using namespace std;
using namespace::std;
// using::Amortization; // dmh. no need for this
// default constructor implementation
Amortization::Amortization()
{
P = 0;
I = 0;
N = 360;
}
Amortization::Amortization(double p, double i, double n)
{
P = p;
I = i;
N = n;
}
// dmh. In Amortization.h you declared this with 2 parameters. The declaration and this
// definition must match
double
Amortization::getPayment() // dmh no semicolon between definition of function and it's body of code
{
// algorithm for amortization
// Payment = P [I * (1 + I)^N] / (1 + I)^N - 1]
// Payment = P [I * powerPart] / [powerPart - 1]
// dmh. Don't change the member I. Otherwise think what would happen if you called getPayment() twice,
// the second call would use the modified version of I. So instead, use a local variable for the modified I.
// I = I / 1200 ;
double interest = I/1200;
double powerPart = pow((1 + interest), N);
// dmh Use "P" instead of "p". Variable names are case sensitive
double payment = P * ((interest * powerPart) / (powerPart - 1));
return payment;
}
//[CourseProjectWeek7Source.cpp]
#include <iostream>
#include <iomanip>
#include <cmath>
// #include "Amortization.h"
#include <fstream>
#include <string>
using namespace std;
using std::ios;
void
menu(void);
void
writeData(void);
void
readData(void);
const char FileName[] = "TestAddress.txt";
int
main()
{
// double Amortization::amort; // dmh you can't define a member of a class, only an instance of the class itself.
double
principle,
interest,
periods;
// dmh You can't call getPayment on a class instance that you haven't defined. And there's no point defining
// it since you haven't entered the principal, interest, and number of periods
// cout << "Your default payment is $" << setprecision(2) << fixed << amort.getPayment() << " endl"; // dmh. To call getPayment, add "()" after the name
// dmh check your spelling in the next few lines
cout << " enter principle: ";
cin >> principle;
cout << "enter interest: ";
cin >> interest;
cout << "enter number of perisods: ";
cin >> periods;
Amortization amort2(principle, interest, periods);
// dmh use "()" to call getPayment
cout << "Your payment is $" << amort2.getPayment() << endl;
cin.clear();
cin.ignore();
cin.get();
menu();
system("Pause");
return 0;
} //end main
void
menu(void)
{
//allow user to choose to append records, display records or exit the program
int
menuChoice = 0;
cout << "1. Display Data" << endl;
cout << "2. Write Data" << endl;
cout << "0. Exit" << endl;
cin >> menuChoice;
if (menuChoice == 1) {
readData();
} else if (menuChoice == 2) {
writeData();
}
} //end menu
void
writeData(void)
{
string
loan;
string
rate;
string
years;
string
userChoice = "Y";
//Write the Address Info to a file
//loop while user still has data to write to file
//eg outStream<<name<<”#”; //where # is the delimiter
ofstream
myFile;
myFile.open(FileName, ios::app);
while (userChoice == "Y") {
// loop to get user input
cin.ignore();
cout << "Append Records" << endl << endl;
cout << "Loan.......... ";
getline(cin, loan);
cout << "Rate.........";
getline(cin, rate);
cout << "Years.........";
getline(cin, years);
cout << "Balance.........";
// format
// John Smith, 902 Union Ave, Any Town, TX, 79552
myFile << loan << "," << rate << "," << years << "," << endl;
cout << "Enter another Record? (Y/N) ";
cin >> userChoice;
}
myFile.close();
} //end write data
void
readData(void)
{
//read data from a file
//use the split function to break a
//deliminated line of text into fields
ifstream
inMyStream(FileName);
if (inMyStream.is_open()) {
//set character to use as a line between record displays
string
recBreaks = "";
recBreaks.assign(20, '-');
int
fieldCount = 0; //keep track of the number of fields read
int
recordCount = 1; //keep track of the number of records read
//read the first field
fieldCount = 1;
string
fieldBuffer;
getline(inMyStream, fieldBuffer, ',');
while (!inMyStream.eof()) {
//display the field
switch (fieldCount) {
case 1:
cout << recBreaks << endl;
cout << "record # " << recordCount << endl;
cout << "Loan...." << fieldBuffer << endl;
break;
case 2:
cout << "Rate.." << fieldBuffer << endl;
break;
case 3:
cout << "Years...." << fieldBuffer << endl;
fieldCount = 0;
recordCount++;
break;
}
//read the next field
getline(inMyStream, fieldBuffer, ',');
fieldCount++;
}
cout << recBreaks << endl;
inMyStream.close();
}
} //end read data
|