Problems with reading from a sequential file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Problems with reading from a sequential file

 
0
  #1
Sep 3rd, 2008
Hi All,
I am having problems with reading input from a sequetail file and I can't figure out what is wrong. Can someone please help me out, I don't know if I am even doing it right, very new at this.

The code below is how I generated my .txt file or maybe I should have just opened word, put in the numbers and just saved it as a .txt file. I don't know but want to include all that I did. I had to use term1, term2, term3, ect. before it would put all the values into my .txt file, maybe this is part of my problem also.

  1. // Fig. 17.25: MC CreateSequentialFile.cpp
  2. // Create a sequential file.
  3. #include <iostream>
  4. using std::cerr;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::ios;
  9.  
  10. #include <fstream> // file stream
  11. using std::ofstream; // output file stream
  12.  
  13. #include <cstdlib>
  14. using std::exit; // exit function prototype
  15.  
  16. int main()
  17. {
  18. // ofstream constructor opens file
  19. ofstream outMCSequentialFile( "calculations.txt", ios::out );
  20.  
  21. // exit program if unable to create file
  22. if ( !outMCSequentialFile ) // overloaded ! operator
  23. {
  24. cerr << "File could not be opened" << endl;
  25. exit( 1 );
  26. } // end if
  27.  
  28. cout << "Please enter term in years." << endl;
  29. double term1;
  30. double term2;
  31. double term3;
  32.  
  33. // read different terms from cin, then place in file
  34. cin >> term1 >> term2 >> term3;
  35. {
  36. outMCSequentialFile << term1 << ' ' << term2 << ' ' << term3 << endl;
  37. cout << "? ";
  38. } // end while
  39. cout << "Enter Interest Rate." << endl
  40. << "Enter end-of-file to end input.\n? ";
  41.  
  42. double intRate1;
  43. double intRate2;
  44. double intRate3;
  45.  
  46. // read interest rates from cin, then place in file
  47. cin >> intRate1>> intRate2 >> intRate3;
  48. {
  49. outMCSequentialFile << intRate1 << ' ' << intRate2 << ' ' << intRate3 << endl;
  50. cout << "? ";
  51. } // end while
  52.  
  53.  
  54. return 0; // ofstream destructor closes file
  55. } // end main
My array table:

7 15 30
5.35 5.5 5.75

Below is my main program. If I enter menu selection 1 the program works just like it should and all is fine there. It's when I enter menu selection 2 that I have all the problems.
I can't get it to display the payent amount or the amortization table at all. Like I said, I really don't know how to do this but have to learn, it seems like C++ is a good progaming language but learning it has been hard for me.

  1. /* Week 4 Individual Assignment
  2. PRG 411
  3. Written By: Ken Volkman
  4. Date: 09/06/2008
  5. File Name: MC_PRG411_WK4
  6. Purpose: To calculate the amount a Mortgage Payement will be from
  7. user input values and to show the amortization schedule. Also it will let
  8. the user to select from a pre-determined menu of rates.
  9. */
  10. #include <iostream> // Library defines facilities for basic I/O operations...
  11. #include <iomanip> // Libaray for stream manipulators...
  12. #include <string> // Library for accepting stings...
  13. #include <limits> // Library for setting limits...
  14. #include <cmath>
  15. #include <fstream>
  16. #include <stdlib.h>
  17.  
  18. using namespace std; // Standard using definitions...
  19.  
  20. class MortgageCal
  21. {
  22. private:
  23. double intYear; // interest rate per year...
  24. double intMonth; // interest converted to months...
  25. double loanAmt; // amount of the loan...
  26. double term; // term in years...
  27. double numMonths; // term converted to months...
  28. double paymentAmt; // variable for monthly payment...
  29. double balance; // running total...
  30. double principleAmt;// amount towards loan...
  31. double loanBal;
  32.  
  33. public:
  34.  
  35. // Constructor, initializes any unpassed value to zero...
  36. MortgageCal(
  37. double intYear = 0,
  38. double term = 0,
  39. double loanAmt = 0,
  40. double paymentAmt = 0,
  41. double numMonths = 0,
  42. double intMonth = 0,
  43. double balance = 0,
  44. double principleAmt = 0,
  45. double loanBal = 0):
  46. intYear(intYear),
  47. term(term),
  48. loanAmt(loanAmt),
  49. paymentAmt(paymentAmt),
  50. numMonths(),
  51. intMonth(),
  52. balance(),
  53. principleAmt(),
  54. loanBal(){ }
  55.  
  56. /// get and set for data elements...
  57. double getnumMonths() { return term * 12; }
  58. bool setnumMonths(double months)
  59. {
  60. if (months > 0) { term = months/12; }
  61. else { return false; }
  62. return true;
  63. }
  64.  
  65. double getTermInYears() { return term; }
  66. bool setTermInYears(double years)
  67. {
  68. if (years > 0) { term = years; }
  69. else { return false; }
  70. return true;
  71. }
  72. double getPrinciple() { return principleAmt;}
  73. bool setPrinciple(double principle)
  74. {
  75. if (principle >= 0) { principleAmt = principle; }
  76. else { return false; }
  77. return true;
  78. }
  79. double getLoanAmount(){ return loanAmt; }
  80. bool setLoanAmount(double loan)
  81. {
  82. if (loan >= 0) { loanAmt = loan; }
  83. else { return false; }
  84. return true;
  85. }
  86.  
  87. double getYearlyRate() { return intYear; }
  88. bool setYearlyRate(double rate)
  89. {
  90. intYear = rate;
  91. return true;
  92. }
  93.  
  94. double getPaymentAmount() { return paymentAmt; }
  95. bool setPaymentAmount (double payment)
  96. {
  97. return true;
  98. }
  99. double getLoanBalance() { return loanBal;}
  100. bool setLoanBalance(double loanB)
  101. {
  102. if (loanB >= 0) { loanBal = loanBal-principleAmt; }
  103. else { return false; }
  104. return true;
  105. }
  106. double getInterestMonth() { return intMonth = (intYear/1200);}
  107. bool setInterestMonth(double interest)
  108. {
  109. return true;
  110. }
  111.  
  112. //Declare other member functions to be defined later...
  113. void Amortization();
  114. void Schedule();
  115. void caseSchedule();
  116. void enterLoanAmts();
  117.  
  118. }; // end class
  119.  
  120. // function to get input from the user...
  121. double getRangedInput(const string prompt, double min, double max, int tries = 5);
  122. void pause(); //pause
  123. int menu = 0;
  124. // Controls the main logic needed to interface with the user...
  125. int main()
  126. {
  127. double loanAmount; // Initialize original amount of loan...
  128. double term; // Initialize term of loan in years...
  129. double numMonths;
  130. double intMonth;
  131. double paymentAmt;
  132. char indicator;
  133.  
  134. MortgageCal morgCalc; // Redefine class as subclass...
  135. do
  136. {
  137. system("cls");
  138. cout << endl << "\"Welcome to our Mortgage Calculator!\"";
  139. cout << "\n\n-------- Main Menu ---------";
  140. cout << "\n 1. Would you like to enter rate and term ";
  141. cout << "\n 2. Would you like to select loan from menu ";
  142. cout << "\n----------------------------\n";
  143. cout << "Press 1 or 2: ";
  144. cin >>menu;
  145.  
  146. //Loop after selecting type of loan
  147. if (menu == 1)
  148. {
  149. morgCalc.enterLoanAmts();
  150. }
  151. else if (menu == 2)
  152. {
  153. morgCalc.caseSchedule();
  154.  
  155. /*cout << "After Amortinzation:\n"
  156. << endl
  157. << "Your payments will be: $" << paymentAmt; //MortgageCal.getPaymentAmount();*/
  158.  
  159. //morgCalc.Amortization();
  160. //morgCalc.Schedule();
  161. }
  162. cout<< "\nEnter another loan (y)es or (n)o):";
  163. cin>>indicator;
  164. } while(indicator=='y');
  165. return 0;
  166.  
  167. } // end main
  168.  
  169. /**
  170. *Function for getting input from the user.
  171. * If the user enters an invalid string, or if the user enters a value outside of the
  172. * given range then it will give the user an error message...
  173. */
  174. double getRangedInput(const string prompt, double min, double max, int tries)
  175. {
  176. int userTry = 0;
  177. double userInVal = 0;
  178. bool isValidInput;
  179. do {
  180. cout << prompt << ": ";
  181. cin >> userInVal;
  182. if (!cin.good())
  183. {
  184. cout <<endl;
  185. cout << "INPUT ERROR: Please input a proper number!" << endl;
  186. cin.clear();
  187. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  188. isValidInput = false;
  189. } else
  190. {
  191. isValidInput = (userInVal >= min && userInVal <= max);
  192. }
  193. if (!isValidInput)
  194. {
  195. cout << "Please enter a value between " << min << " and " << max << "!" << endl;
  196. cout << endl;
  197. }
  198. } while (!isValidInput && ++userTry < tries); // end while...
  199. if (userTry == tries)
  200. {
  201. cout << "User input invalid, can not process!" << endl;
  202. exit(1); //If the user can't input a valid value, exit the program...
  203. }
  204. return userInVal;
  205.  
  206. } // end of get for error input...
  207.  
  208. void MortgageCal::Schedule()
  209. {
  210. int intPymtNum = 1; // counter for payments
  211. int max = 0; // displays maximum lines on screen
  212. double intRate; // calculated loan interest
  213. double intPayment; // interest amount applied to loan
  214. double principleAmt; // principle amount applied to loan
  215. double loanBal; // running balance of loan
  216.  
  217. cout << endl
  218. << "This is a breakdown of "
  219. << "how much of your payment is applied " << endl;
  220. cout << "to interest and the "
  221. << "balance of your loan: " << endl;
  222. system ( "PAUSE" );
  223. cout << endl << endl; // headings
  224. cout << " Payment" << setw(15) << "Interest"
  225. << endl;
  226. cout << " Number " << setw(15) << " Paid "
  227. << setw(14) << "balance " << endl;
  228. cout << " _______" << setw(15) << "________"
  229. << setw(14) << "________" << endl;
  230.  
  231. loanBal = loanAmt; // used for running total
  232. cout.setf(ios::fixed); // fixed not scientific format
  233. cout.setf(ios::showpoint); // trailing 0's
  234. cout.precision(2); // 2 places
  235.  
  236. for(int i = 1, max = 1; intPymtNum <= numMonths; i++)
  237. {
  238. // controls the loop of monthly payments
  239. intRate = intMonth; //intMonth;
  240. intPayment = loanBal * intRate;
  241. principleAmt = paymentAmt - (loanBal * intRate);
  242. loanBal = loanBal - principleAmt;
  243. if (loanBal <= 0)
  244. {
  245. loanBal = 0; // to avoid negative numbers
  246. }
  247. cout << endl
  248. << setw(5) << intPymtNum << setw(17)
  249. << intPayment << setw(15) << loanBal;
  250. intPymtNum++; // adds 1 to payment number
  251. max++; // adds 1 to line count
  252. if (max == 20) // loop pauses program every 19 lines
  253. {
  254. cout << endl;
  255. system ( "PAUSE" );
  256. max = 1; // resets line count
  257. cout << endl << endl; // prints headings again
  258. cout << " Payment" << setw(15) << "Interest"
  259. << endl;
  260. cout << " Number " << setw(15) << " Paid "
  261. << setw(14) << "balance " << endl;
  262. cout << " _______" << setw(15) << "________"
  263. << setw(14) << "________" << endl;
  264. } // end if loop
  265. } // end for loop
  266. } // end MortgageCal::Schedule
  267.  
  268. void pause()
  269. {
  270. cout << "Press enter to continue...";
  271. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  272. cin.ignore();
  273. }
  274.  
  275. /*define member functions...
  276. Determines monthly interest in decimal format
  277. Set up monthly payment calculations...*/
  278.  
  279. void MortgageCal::Amortization()
  280. {
  281. numMonths = term * 12; // Converts years to months...
  282. intMonth = intYear/1200; // Converts annual rate to monthly rate...
  283. paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths)); // Amortization formula...
  284. }
  285.  
  286. void MortgageCal::caseSchedule()
  287. {
  288. //Local Variables
  289. int checklist = 0; //Check for if loop to make sure proper entry
  290. int choice=0; //Verify choice of loan and send to array
  291. int ary; //Choice of array from if loop
  292.  
  293. int iarray[3] = {0};
  294. float farray[3] = {0.0F};
  295. ifstream in("calculations");
  296. in >> iarray[0] >> iarray[1] >> iarray[2];
  297. in >> farray[0] >> farray[1] >> farray[2];
  298.  
  299. do
  300. {
  301. //Setup menu for loan to choose which term and interest rate
  302. cout<<"\n------Loan Menu Items------";
  303. cout<< "\n1) 7 years at 5.25% \n2) 15 years at 5.5% \n3) 30 years at 5.75%\n-------------------------\nEnter (1-3)";
  304. cin >> choice;
  305. checklist = 0; //Reset checklist to 0 on loop
  306.  
  307. if(choice <= 0) //Error if user chooses 0 on loop
  308. {
  309. cout<<"\n\n\n";
  310. cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
  311. checklist = 1;
  312. }
  313. else if(choice == 1) //Send to array
  314. {
  315. ary=0;
  316. }
  317. else if(choice == 2) //Send to array
  318. {
  319. ary=1;
  320. }
  321. else if (choice ==3) //Send to array
  322. {
  323. ary=2;
  324. }
  325. else if (choice > 3) //Error if user chooses a number greater then 3
  326. {
  327. cout<<"\n\n\n";
  328. cout<<"\n\nError! Please enter only 1, 2, or 3 to select the term and rate\n";
  329. checklist = 1;
  330. }
  331. cout<<"Please enter the Loan Amount:$";
  332. cin >> loanAmt;
  333. }
  334. while (checklist == 1);
  335. numMonths = (iarray[ary] * 12); //Set Term for Mortgage Calculation
  336. intMonth = (farray[ary]/1200); //Set Interest for Mortgage Calculation
  337. paymentAmt = (loanAmt*intMonth)/(1-pow(1+intMonth,-numMonths)); // Amortization formula...
  338.  
  339. cout << "After Amortinzation:\n"
  340. << endl
  341. << "Your payments will be: $" << paymentAmt; //MortgageCal.getPaymentAmount();
  342.  
  343. MortgageCal::Schedule();
  344. } // End MortgageCal::caseSchedule
  345.  
  346. void MortgageCal::enterLoanAmts()
  347. {
  348. double loanAmount;
  349. double yearlyRate;
  350.  
  351. MortgageCal morgCalc;
  352. char indicator = ('y', 'Y'); // set indicator to 'y' for continue...
  353. while ((indicator =='y') || (indicator == 'Y')) // to continue loop as long as input = y
  354. {
  355. /*get and set all of the input from the user/ranges...
  356. Set up screen output to display mortgage amount, interest
  357. rate and term of loan...*/
  358. loanAmount = getRangedInput("Please enter the loan amount", 0, numeric_limits<double>::max());
  359. cout << endl;
  360. term = getRangedInput("Please enter the loan term in years", 0, 10000);
  361. cout << endl;
  362. yearlyRate = getRangedInput("Please enter the yearly interest rate", 0, 100);
  363. cout << endl;
  364. morgCalc.setLoanAmount(loanAmount);
  365. morgCalc.setTermInYears(term);
  366. morgCalc.setYearlyRate(yearlyRate);
  367.  
  368. //display the payment calculations...
  369. morgCalc.Amortization();
  370.  
  371. cout << "After Amortinzation:\n"
  372. << endl
  373. << "Your payments will be: $" << morgCalc.getPaymentAmount();
  374.  
  375. morgCalc.Schedule();
  376. cout << endl;
  377. pause(); // to get user input to enter new values...
  378. cout << endl; // Enter newline...
  379. //Set up screen for input on whether to enter new numbers or exit...
  380. cout << "Do you want to enter another value (enter y to continue or n to end)? ";
  381. cin >> indicator; // Read indicator input...
  382. } //end while
  383. cout << endl;
  384. cout << "\"Thank you for using our mortgage calculator\"" << endl;
  385. cout << endl;
  386. //cout<< "\nEnter another loan (y)es or (n)o):";
  387. //cin>>indicator;
  388. pause();
  389. } // End MortgageCal::enterLoanAmts()

I had the code that I selected from a switch statement to get the variables and that worked fine too. The code may not have been the best but it worked. But this time I have to read from a sequential file for the variables to get my data.
Please help and Thanks,
Ken
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Problems with reading from a sequential file

 
0
  #2
Sep 3rd, 2008
Two tips: first, you have commented that there is an end to a while loop where the three temps are entered, but there is no loop and I have had problems using fstream. You can declare it as "fstream myFileInput("input.txt"); without the ios::out.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: Problems with reading from a sequential file

 
0
  #3
Sep 3rd, 2008
Thank you for your help.
I just wanted to let everyone know that I did get it figured out.
I would still like some input as to what you think is wrong with the program in general. I am new to this and am always looking for better ways to do things,
Thanks,
Ken
Reply With Quote Quick reply to this message  
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 Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Problems with reading from a sequential file

 
0
  #5
Sep 4th, 2008
My personal preference is to declare variables of the same type in the same declaration, such as double loanAmount, term, numMonths, intMonth, double paymentAmt; which makes the code a little shorter and nicer to read. Switch statements can also make it a little simpler than using else if(). Just some ideas
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 25
Reputation: tyserman5674 is an unknown quantity at this point 
Solved Threads: 0
tyserman5674 tyserman5674 is offline Offline
Light Poster

Re: Problems with reading from a sequential file

 
0
  #6
Sep 4th, 2008
Thank you both very much for your input. I found that I had missed the .txt in the file and that did make it work. I am also making the other changes that both of you suggested.
Thanks Again and any other comments that you may have would be appreciated as I would really like to learn this language.
Ken
Reply With Quote Quick reply to this message  
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
  #7
Sep 4th, 2008
C++ isn't as tough as you thought. Just have more practice and stick with this forum. I learned a lot from this forum. Happy coding!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC