View Single Post
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