View Single Post
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Problems with reading from a sequential file

 
0
  #4
Sep 4th, 2008
You definitely have to work on your alignment of your coding. The alignment and programming style should be consistent. The reason why you program is not working when you choose option 2 is because you miss ".txt" in line 292.

Other suggestions:
1. If the Constructor have quite a few numbers of variable to initialize, it is better to declare it outside of the class declaration. The same should be applied to the declaration of functions.
2. For selection of menu(exp : line 408 & 412), it is better to use "switch()".
3. For case like in line 354 or line 402, it is better is you use something like this:
while (tolower(indicator) == 'y') This will convert the indicator to lowercase regardless it is capital letter or lower letter.
4. It is not necessary to declare your own pause function. You can use system("PAUSE"). But using system("") is not advisable as what being told by niek_e. (for info you can refer to http://www.daniweb.com/forums/thread141628.html)

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <limits>
  5. #include <cmath>
  6. #include <fstream>
  7. #include <stdlib.h>
  8. using namespace std;
  9.  
  10.  
  11. class MortgageCal
  12. {
  13. private:
  14. double intYear;
  15. double intMonth;
  16. double loanAmt;
  17. double term;
  18. double numMonths;
  19. double paymentAmt;
  20. double balance;
  21. double principleAmt;
  22. double loanBal;
  23.  
  24. public:
  25. MortgageCal(
  26. double intYear = 0,
  27. double term = 0,
  28. double loanAmt = 0,
  29. double paymentAmt = 0,
  30. double numMonths = 0,
  31. double intMonth = 0,
  32. double balance = 0,
  33. double principleAmt = 0,
  34. double loanBal = 0):
  35.  
  36. intYear(intYear),
  37. term(term),
  38. loanAmt(loanAmt),
  39. paymentAmt(paymentAmt),
  40. numMonths(),
  41. intMonth(),
  42. balance(),
  43. principleAmt(),
  44. loanBal(){ }
  45.  
  46. double getnumMonths()
  47. {
  48. return term * 12;
  49. }
  50.  
  51. bool setnumMonths(double months)
  52. {
  53. if (months > 0)
  54. {
  55. term = months/12;
  56. }
  57.  
  58. else
  59. {
  60. return false;
  61. }
  62.  
  63. return true;
  64. }
  65.  
  66. double getTermInYears() { return term; }
  67. bool setTermInYears(double years)
  68. {
  69. if (years > 0)
  70. {
  71. term = years;
  72. }
  73.  
  74. else
  75. {
  76. return false;
  77. }
  78.  
  79. return true;
  80. }
  81.  
  82. double getPrinciple()
  83. {
  84. return principleAmt;
  85. }
  86.  
  87. bool setPrinciple(double principle)
  88. {
  89. if (principle >= 0)
  90. {
  91. principleAmt = principle;
  92. }
  93.  
  94. else
  95. {
  96. return false;
  97. }
  98.  
  99. return true;
  100.  
  101. }
  102.  
  103.  
  104. double getLoanAmount()
  105. {
  106. return loanAmt;
  107. }
  108.  
  109. bool setLoanAmount(double loan)
  110. {
  111. if (loan >= 0)
  112. {
  113. loanAmt = loan;
  114. }
  115.  
  116. else
  117. {
  118. return false;
  119. }
  120.  
  121. return true;
  122. }
  123.  
  124. double getYearlyRate()
  125. {
  126. return intYear;
  127. }
  128.  
  129. bool setYearlyRate(double rate)
  130. {
  131. intYear = rate;
  132. return true;
  133. }
  134.  
  135. double getPaymentAmount()
  136. {
  137. return paymentAmt;
  138. }
  139.  
  140. bool setPaymentAmount (double payment)
  141. {
  142. return true;
  143. }
  144.  
  145. double getLoanBalance()
  146. {
  147. return loanBal;
  148. }
  149.  
  150. bool setLoanBalance(double loanB)
  151. {
  152. if (loanB >= 0)
  153. {
  154. loanBal = loanBal-principleAmt;
  155. }
  156.  
  157. else
  158. {
  159. return false;
  160. }
  161.  
  162. return true;
  163. }
  164.  
  165. double getInterestMonth()
  166. {
  167. return intMonth = (intYear/1200);
  168. }
  169.  
  170. bool setInterestMonth(double interest)
  171. {
  172. return true;
  173. }
  174.  
  175. void Amortization();
  176. void Schedule();
  177. void caseSchedule();
  178. void enterLoanAmts();
  179.  
  180. };
  181.  
  182. double getRangedInput(const string prompt, double min, double max, int tries = 5);
  183. void pause();
  184. int menu = 0;
  185.  
  186. double getRangedInput(const string prompt, double min, double max, int tries)
  187. {
  188. int userTry = 0;
  189. double userInVal = 0;
  190. bool isValidInput;
  191. do
  192. {
  193. cout << prompt << ": ";
  194. cin >> userInVal;
  195. if (!cin.good())
  196. {
  197. cout <<endl;
  198. cout << "INPUT ERROR: Please input a proper number!" << endl;
  199. cin.clear();
  200. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  201. isValidInput = false;
  202. }
  203.  
  204. else
  205. {
  206. isValidInput = (userInVal >= min && userInVal <= max);
  207. }
  208.  
  209. if (!isValidInput)
  210. {
  211. cout << "Please enter a value between " << min << " and " << max << "!" << endl;
  212. cout << endl;
  213. }
  214.  
  215. } while (!isValidInput && ++userTry < tries);
  216.  
  217. if (userTry == tries)
  218. {
  219. cout << "User input invalid, can not process!" << endl;
  220. exit(1);
  221. }
  222.  
  223. return userInVal;
  224.  
  225. }
  226.  
  227. void MortgageCal::Schedule()
  228. {
  229. int intPymtNum = 1;
  230. int max = 0;
  231. double intRate;
  232. double intPayment;
  233. double principleAmt;
  234. double loanBal;
  235.  
  236. cout << endl << "This is a breakdown of how much of your payment is applied to interest and the balance of your loan: " << endl;
  237. system ( "PAUSE" );
  238.  
  239. cout << endl << endl;
  240. cout << " Payment" << setw(15) << "Interest" << endl;
  241. cout << " Number " << setw(15) << " Paid " << setw(14) << "balance " << endl;
  242. cout << " _______" << setw(15) << "________" << setw(14) << "________" << endl;
  243.  
  244. loanBal = loanAmt; l
  245. cout.setf(ios::fixed);
  246. cout.setf(ios::showpoint);
  247. cout.precision(2);
  248.  
  249. for(int i = 1, max = 1; intPymtNum <= numMonths; i++)
  250. {
  251. intRate = intMonth;
  252. intPayment = loanBal * intRate;
  253. principleAmt = paymentAmt - (loanBal * intRate);
  254. loanBal = loanBal - principleAmt;
  255.  
  256. if (loanBal <= 0)
  257. {
  258. loanBal = 0;
  259. }
  260.  
  261. cout << endl << setw(5) << intPymtNum << setw(17) << intPayment << setw(15) << loanBal;
  262. intPymtNum++;
  263. max++;
  264.  
  265. if (max == 20)
  266. {
  267. cout << endl;
  268. system ( "PAUSE" );
  269. max = 1;
  270. cout << endl << endl;
  271. cout << " Payment" << setw(15) << "Interest" << endl;
  272. cout << " Number " << setw(15) << " Paid " << setw(14) << "balance " << endl;
  273. cout << " _______" << setw(15) << "________" << setw(14) << "________" << endl;
  274. }
  275. }
  276. }
  277.  
  278. void pause()
  279. {
  280. cout << "Press enter to continue...";
  281. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  282. cin.ignore();
  283. }
  284.  
  285. void MortgageCal::Amortization()
  286. {
  287. numMonths = term * 12;
  288. intMonth = intYear/1200;
  289. paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths));
  290. }
  291.  
  292. void MortgageCal::caseSchedule()
  293. {
  294. int checklist = 0;
  295. int choice=0;
  296. int ary;
  297.  
  298. int iarray[3] = {0};
  299. float farray[3] = {0.0F};
  300. ifstream in("calculations.txt");
  301. in >> iarray[0] >> iarray[1] >> iarray[2];
  302. in >> farray[0] >> farray[1] >> farray[2];
  303.  
  304. do
  305. {
  306. cout<<"\n------Loan Menu Items------";
  307. cout<< "\n1) 7 years at 5.25% \n2) 15 years at 5.5% \n3) 30 years at 5.75%\n-------------------------\nEnter (1-3)";
  308. cin >> choice;
  309. checklist = 0;
  310.  
  311. if(choice <= 0)
  312. {
  313. cout<<"\n\n\n";
  314. cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
  315. checklist = 1;
  316. }
  317.  
  318. else if(choice == 1)
  319. {
  320. ary=0;
  321. }
  322.  
  323. else if(choice == 2)
  324. {
  325. ary=1;
  326. }
  327.  
  328. else if (choice ==3)
  329. {
  330. ary=2;
  331. }
  332.  
  333. else if (choice > 3)
  334. {
  335. cout<<"\n\n\n";
  336. cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
  337. checklist = 1;
  338. }
  339.  
  340. cout<<"Please enter the Loan Amount:$";
  341. cin >> loanAmt;
  342. } while (checklist == 1);
  343.  
  344. numMonths = (iarray[ary] * 12);
  345. intMonth = (farray[ary]/1200);
  346. paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths));
  347.  
  348. cout << "After Amortinzation:\n" << endl << "Your payments will be: $" << paymentAmt;
  349.  
  350. MortgageCal::Schedule();
  351. }
  352.  
  353. void MortgageCal::enterLoanAmts()
  354. {
  355. double loanAmount;
  356. double yearlyRate;
  357.  
  358. MortgageCal morgCalc;
  359. char indicator = ('y', 'Y')
  360. while ((indicator =='y') || (indicator == 'Y'))
  361. {
  362. loanAmount = getRangedInput("Please enter the loan amount", 0, numeric_limits<double>::max());
  363. term = getRangedInput("\nPlease enter the loan term in years", 0, 10000);
  364. yearlyRate = getRangedInput("\nPlease enter the yearly interest rate", 0, 100);
  365. cout << endl;
  366. morgCalc.setLoanAmount(loanAmount);
  367. morgCalc.setTermInYears(term);
  368. morgCalc.setYearlyRate(yearlyRate);
  369.  
  370. morgCalc.Amortization();
  371.  
  372. cout << "After Amortinzation:\n" << endl;
  373. cout << "Your payments will be: $" << morgCalc.getPaymentAmount();
  374.  
  375. morgCalc.Schedule();
  376. cout << endl;
  377. pause();
  378. cout << endl;
  379.  
  380. cout << "Do you want to enter another value (enter y to continue or n to end)? ";
  381. cin >> indicator;
  382.  
  383. }
  384.  
  385. cout << endl;
  386. cout << "\"Thank you for using our mortgage calculator\"" << endl;
  387. cout << endl;
  388.  
  389. pause();
  390. }
  391.  
  392. int main()
  393. {
  394. double loanAmount;
  395. double term;
  396. double numMonths;
  397. double intMonth;
  398. double paymentAmt;
  399. char indicator;
  400.  
  401. MortgageCal morgCalc;
  402.  
  403. do
  404. {
  405. system("cls");
  406. cout << endl << "\"Welcome to our Mortgage Calculator!\"";
  407. cout << "\n\n-------- Main Menu ---------";
  408. cout << "\n 1. Would you like to enter rate and term ";
  409. cout << "\n 2. Would you like to select loan from menu ";
  410. cout << "\n----------------------------\n";
  411. cout << "Press 1 or 2: ";
  412. cin >>menu;
  413.  
  414. if (menu == 1)
  415. {
  416. morgCalc.enterLoanAmts();
  417. }
  418. else if (menu == 2)
  419. {
  420. morgCalc.caseSchedule();
  421. }
  422.  
  423. cout<< "\nEnter another loan (y)es or (n)o):";
  424. cin>>indicator;
  425.  
  426. } while(indicator == 'y');
  427.  
  428. return 0;
  429.  
  430. }

Happy Coding...
Reply With Quote