Array and Switch issues

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

Join Date: Mar 2007
Posts: 4
Reputation: -_00_- is an unknown quantity at this point 
Solved Threads: 0
-_00_- -_00_- is offline Offline
Newbie Poster

Array and Switch issues

 
1
  #1
Apr 23rd, 2007
I cannot figure out why my program gets the wrong output but I am assuming it has something to do with the array or the switch statement. Can someone guide me in the right direction? I am at a point where I am making no progress right now and now ask for help. Thanks in advance.


  1. /*
  2. Write the program as a procedural C++ program. Allow the user to input
  3. the amount of a mortgage and then select from a menu of mortgage loans:
  4.  
  5. 7 year at 5.35%
  6. 15 year at 5.5%
  7. 30 year at 5.75%
  8.  
  9. Use an array for the different loans. Display the mortgage payment amount.
  10. Then, list the loan balance and interest paid for each payment over the term
  11. of the loan. On longer-term loans, the list will scroll off the screen.
  12. Do not allow the list to scroll off the screen, but rather display a partial
  13. list and then allow the user to continue the list. Allow the user to loop back
  14. and enter new data or quit. Insert comments in the program to document the
  15. program.
  16. */
  17.  
  18. #include <math.h> // Math Constant
  19. #include <cstdlib> // Standard Library
  20. #include <iostream> // Defines facilities for basic I/O operations
  21. using namespace std; // Namespace standard library
  22.  
  23. int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
  24. {
  25. cout << " " << endl; // Empty line before assignment title
  26. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  27. cout << " // // " << endl;
  28. cout << " // Calculate and display the mortgage payment amount using the amount // " << endl;
  29. cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl;
  30. cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl;
  31. cout << " // and will select from a menu the amount of years and interest rate. // " << endl;
  32. cout << " // The mortgage payment will display and the ammortization table will // " << endl;
  33. cout << " // display the loan balance and interest paid. The user will be // " << endl;
  34. cout << " // allowed to either continue the ammortization table, enter new // " << endl;
  35. cout << " // loan data or quit the program altogether. // " << endl;
  36. cout << " // // " << endl;
  37. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  38. cout << " " << endl << endl << endl << endl << endl << endl;
  39.  
  40. double loanAmount, // Amount of the Loan
  41. monthlyPayment, // Monthly Payment of the Loan
  42. loanBalance, // Balance of the Loan
  43. intPaid, // Interest Paid on the Loan
  44. amountPaid, // Amount Paid on the Loan
  45. intRate[3]={5.25,
  46. 5.50,
  47. 5.75}; // Interest Rate of the Loan
  48.  
  49. int numPayments, // Number of Payments
  50. paymentCounter,
  51. dividelist = 0, // Display partial list
  52. length[3]={7, 15, 30}; // Length of the Loan
  53.  
  54. char listmore;
  55. char quit; // Prompt user to quit
  56. quit = 'C'; // Quit variable
  57.  
  58. while (quit != 'Q' && quit != 'q')
  59. {
  60. cout << "\t Enter Loan Amount (*NUMBERS ONLY*): ";
  61. cin >> loanAmount;
  62. cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl;
  63. cout << "\t 1. 7 Years at 5.35% " << endl;
  64. cout << "\t 2. 15 Years at 5.50% " << endl;
  65. cout << "\t 3. 30 Years at 5.75% " << endl;
  66. cout << "\t Enter Line Number Here: ";
  67.  
  68. int choice;
  69. cin >> choice;
  70. cout << endl;
  71.  
  72. int i;
  73. for (i = 0; i < 1; i++)
  74. switch (choice)
  75. {
  76. case 1:
  77. cout << "\t You Selected 7 Years at 5.35%" << endl;
  78. break;
  79. case 2:
  80. cout << "\t You Selected 15 Years at 5.50%" << endl;
  81. break;
  82. case 3:
  83. cout << "\t You Selected 30 Years at 5.75%" << endl;
  84. break;
  85. default:
  86. cout << "\t Invalid Line Number" << endl;
  87. // Still having minor issues with invalid number
  88. if (choice != 1,2,3)
  89. {
  90. cout << "\t Enter Line Number Here: ";
  91. int choice;
  92. cin >> choice;
  93. cout << endl;
  94. if (choice != 1,2,3)
  95. {
  96. cout << "\t You cannot follow directions. " << endl;
  97. case 9:
  98. cout << "\t Program will now END " << endl;
  99. break;
  100. }
  101. }
  102. }
  103.  
  104. cout << endl;
  105.  
  106. monthlyPayment = loanAmount * (intRate[choice] / 1200) /
  107. (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));
  108.  
  109. cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl;
  110. numPayments = length[choice] * 12;
  111. dividelist = 0;
  112.  
  113. for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
  114. {
  115. // Math Formula for intPaid to set up value for amountPaid
  116. intPaid = loanAmount * (intRate[choice] / 1200);
  117. amountPaid = monthlyPayment - intPaid;
  118. loanBalance = loanAmount - amountPaid;
  119.  
  120. if (loanBalance < 0)loanBalance = 0;
  121. loanAmount = loanBalance;
  122.  
  123. if (dividelist == 0)
  124. {
  125. cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
  126. cout << "\t\t____________" << "\t\t_____________" << endl << endl;
  127. }
  128.  
  129. cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
  130. ++dividelist;
  131.  
  132. // User to Continue, enter New Data or Quit
  133. if (dividelist == 12)
  134. {
  135. cout << endl << endl;
  136. cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
  137. cin >> listmore;
  138. if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
  139. else if ((listmore == 'N')||(listmore == 'n'))
  140. break; // Halt Current Program if New Data is entered, verify and restart
  141. else if ((listmore == 'Q')||(listmore == 'q'))
  142. return 0; // End Program
  143. }
  144. }
  145.  
  146. do // This loops valididates user input to enter New Data
  147. {
  148. cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
  149. cin >> quit;
  150. cout << "\n";
  151. }
  152.  
  153. while ((quit != 'q') &&
  154. (quit != 'Q') &&
  155. (quit != 'c') &&
  156. (quit != 'C'));
  157. }
  158.  
  159. cout << endl;
  160. system("PAUSE"); // Pause program prior to exit
  161. return EXIT_SUCCESS; // Exit
  162. }
Last edited by -_00_-; Apr 23rd, 2007 at 3:20 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Array and Switch issues

 
0
  #2
Apr 23rd, 2007
Who knows how much, if anything this will help, but the user can select a value for a variable called choice which can range from 1-3 and is used as an index in an array that has three elements which can have indexe ranging from 0-2, so when user selects 3 it causes an out of bounds problem when used as an index.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: -_00_- is an unknown quantity at this point 
Solved Threads: 0
-_00_- -_00_- is offline Offline
Newbie Poster

Re: Array and Switch issues

 
0
  #3
Apr 23rd, 2007
I understand what you are saying. I noticed that when I enter 2 for the 15yrs 5.50%, I get the result for 30yrs and 5.75%. I tried to modify this switch to show case 0 but that did not seem to work. At least now it displays that I entered in what line I intended, it is just that I am still getting the result for loan #3. WHen I enter loan #3, I get the $1.#INF as a monthly payment. I really am not making much progress on this.

  1. /*
  2. Write the program as a procedural C++ program. Allow the user to input
  3. the amount of a mortgage and then select from a menu of mortgage loans:
  4.  
  5. 7 year at 5.35%
  6. 15 year at 5.5%
  7. 30 year at 5.75%
  8.  
  9. Use an array for the different loans. Display the mortgage payment amount.
  10. Then, list the loan balance and interest paid for each payment over the term
  11. of the loan. On longer-term loans, the list will scroll off the screen.
  12. Do not allow the list to scroll off the screen, but rather display a partial
  13. list and then allow the user to continue the list. Allow the user to loop back
  14. and enter new data or quit. Insert comments in the program to document the
  15. program.
  16. */
  17.  
  18. #include <math.h> // Math Constant
  19. #include <cstdlib> // Standard Library
  20. #include <iostream> // Defines facilities for basic I/O operations
  21. using namespace std; // Namespace standard library
  22.  
  23. int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
  24. {
  25. cout << " " << endl; // Empty line before assignment title
  26. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  27. cout << " // // " << endl;
  28. cout << " // Calculate and display the mortgage payment amount using the amount // " << endl;
  29. cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl;
  30. cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl;
  31. cout << " // and will select from a menu the amount of years and interest rate. // " << endl;
  32. cout << " // The mortgage payment will display and the ammortization table will // " << endl;
  33. cout << " // display the loan balance and interest paid. The user will be // " << endl;
  34. cout << " // allowed to either continue the ammortization table, enter new // " << endl;
  35. cout << " // loan data or quit the program altogether. // " << endl;
  36. cout << " // // " << endl;
  37. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  38. cout << " " << endl << endl << endl << endl << endl << endl;
  39.  
  40. double loanAmount, // Amount of the Loan
  41. monthlyPayment, // Monthly Payment of the Loan
  42. loanBalance, // Balance of the Loan
  43. intPaid, // Interest Paid on the Loan
  44. amountPaid, // Amount Paid on the Loan
  45. intRate[3] = {5.25,
  46. 5.50,
  47. 5.75}; // Interest Rate of the Loan
  48.  
  49. int numPayments, // Number of Payments
  50. paymentCounter,
  51. dividelist = 0, // Display partial list
  52. length[3] = {7, 15, 30}; // Length of the Loan
  53.  
  54. char listmore;
  55. char quit; // Prompt user to quit
  56. quit = 'C'; // Quit variable
  57.  
  58. while (quit != 'Q' && quit != 'q')
  59. {
  60. cout << "\t Enter Loan Amount (*NUMBERS ONLY*): ";
  61. cin >> loanAmount;
  62. cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl;
  63. cout << "\t 1. 7 Years at 5.35% " << endl;
  64. cout << "\t 2. 15 Years at 5.50% " << endl;
  65. cout << "\t 3. 30 Years at 5.75% " << endl;
  66. cout << "\t Enter Line Number Here: ";
  67.  
  68. int choice;
  69. cin >> choice;
  70. cout << endl;
  71.  
  72. int i;
  73. for (i = 0; i < 1; i++)
  74. switch (choice)
  75. {
  76. case 0:
  77. cout << endl;
  78. break;
  79. case 1:
  80. cout << "\t You Selected 7 Years at 5.35%" << endl;
  81. break;
  82. case 2:
  83. cout << "\t You Selected 15 Years at 5.50%" << endl;
  84. break;
  85. case 3:
  86. cout << "\t You Selected 30 Years at 5.75%" << endl;
  87. break;
  88. default:
  89. cout << "\t Invalid Line Number" << endl;
  90. /*
  91.   // Still having minor issues with invalid number
  92.   if (choice != 1,2,3)
  93.   {
  94.   cout << "\t Enter Line Number Here: ";
  95.   int choice;
  96.   cin >> choice;
  97.   cout << endl;
  98.   if (choice != 1,2,3)
  99.   {
  100.   cout << "\t You cannot follow directions. " << endl;
  101.   case 9:
  102.   cout << "\t Program will now END " << endl;
  103.   break;
  104.   }
  105.   }*/
  106. }
  107.  
  108. cout << endl;
  109.  
  110. monthlyPayment = loanAmount * (intRate[choice] / 1200) /
  111. (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));
  112.  
  113. cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl;
  114. double numPayments = length[choice] * 12;
  115. dividelist = 0;
  116.  
  117. for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
  118. {
  119. // Math Formula for intPaid to set up value for amountPaid
  120. intPaid = loanAmount * (intRate[choice] / 1200);
  121. amountPaid = monthlyPayment - intPaid;
  122. loanBalance = loanAmount - amountPaid;
  123.  
  124. if (loanBalance < 0)loanBalance = 0;
  125. loanAmount = loanBalance;
  126.  
  127. if (dividelist == 0)
  128. {
  129. cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
  130. cout << "\t\t____________" << "\t\t_____________" << endl << endl;
  131. }
  132.  
  133. cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
  134. ++dividelist;
  135.  
  136. // User to Continue, enter New Data or Quit
  137. if (dividelist == 12)
  138. {
  139. cout << endl << endl;
  140. cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
  141. cin >> listmore;
  142. if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
  143. else if ((listmore == 'N')||(listmore == 'n'))
  144. break; // Halt Current Program if New Data is entered, verify and restart
  145. else if ((listmore == 'Q')||(listmore == 'q'))
  146. return 0; // End Program
  147. }
  148. }
  149.  
  150. do // This loops valididates user input to enter New Data
  151. {
  152. cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
  153. cin >> quit;
  154. cout << "\n";
  155. }
  156.  
  157. while ((quit != 'q') &&
  158. (quit != 'Q') &&
  159. (quit != 'c') &&
  160. (quit != 'C'));
  161. }
  162.  
  163. cout << endl;
  164. system("PAUSE"); // Pause program prior to exit
  165. return EXIT_SUCCESS; // Exit
  166. }
Last edited by -_00_-; Apr 23rd, 2007 at 6:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Array and Switch issues

 
0
  #4
Apr 23rd, 2007
Allow the user to select choice based on 1, 2, or 3 but when using choice as the index to an array, then use choice - 1 as the index, not choice itself: length[choice - 1] or intRate[choice - 1].
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: -_00_- is an unknown quantity at this point 
Solved Threads: 0
-_00_- -_00_- is offline Offline
Newbie Poster

Re: Array and Switch issues

 
0
  #5
Apr 23rd, 2007
I figured it out!!! OMG! Finally! Now to beautify the output

  1. #include <math.h> // Math Constant
  2. #include <cstdlib> // Standard Library
  3. #include <iostream> // Defines facilities for basic I/O operations
  4. using namespace std; // Namespace standard library
  5.  
  6. int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
  7. {
  8. cout << " " << endl; // Empty line before assignment title
  9. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  10. cout << " // // " << endl;
  11. cout << " // Calculate and display the mortgage payment amount using the amount // " << endl;
  12. cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl;
  13. cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl;
  14. cout << " // and will select from a menu the amount of years and interest rate. // " << endl;
  15. cout << " // The mortgage payment will display and the ammortization table will // " << endl;
  16. cout << " // display the loan balance and interest paid. The user will be // " << endl;
  17. cout << " // allowed to either continue the ammortization table, enter new // " << endl;
  18. cout << " // loan data or quit the program altogether. // " << endl;
  19. cout << " // // " << endl;
  20. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  21. cout << " " << endl << endl << endl << endl << endl << endl;
  22.  
  23. double loanAmount, // Amount of the Loan
  24. monthlyPayment, // Monthly Payment of the Loan
  25. loanBalance, // Balance of the Loan
  26. intPaid, // Interest Paid on the Loan
  27. amountPaid, // Amount Paid on the Loan
  28. intRate[3] = {5.25,
  29. 5.50,
  30. 5.75}; // Interest Rate of the Loan
  31.  
  32. int numPayments, // Number of Payments
  33. paymentCounter,
  34. dividelist = 0, // Display partial list
  35. length[3] = {7, 15, 30}; // Length of the Loan
  36.  
  37. char listmore;
  38. char quit; // Prompt user to quit
  39. quit = 'C'; // Quit variable
  40.  
  41. while (quit != 'Q' && quit != 'q')
  42. {
  43. cout << "\t Enter Loan Amount (*NUMBERS ONLY*): ";
  44. cin >> loanAmount;
  45. cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl;
  46. cout << "\t 1. 7 Years at 5.35% " << endl;
  47. cout << "\t 2. 15 Years at 5.50% " << endl;
  48. cout << "\t 3. 30 Years at 5.75% " << endl;
  49. cout << "\t Enter Line Number Here: ";
  50.  
  51. int choice;
  52. cin >> choice;
  53. cout << endl;
  54.  
  55. int i;
  56. for (i = 0; i < 1; i++)
  57. switch (choice)
  58. {
  59. case 0: cout << endl;
  60. break;
  61. case 1:
  62. length[1] = 7;
  63. intRate[1] = 5.35;
  64. cout << "\t You Selected 7 Years at 5.35%" << endl;
  65. break;
  66. case 2:
  67. length[2] = 15;
  68. intRate[2]= 5.50;
  69. cout << "\t You Selected 15 Years at 5.50%" << endl;
  70. break;
  71. case 3:
  72. length[3] = 30;
  73. intRate[3] = 5.75;
  74. cout << "\t You Selected 30 Years at 5.75%" << endl;
  75. break;
  76. default:
  77. cout << "\t Invalid Line Number" << endl;
  78. /*
  79.   // Still having minor issues with invalid number
  80.   if (choice != 1,2,3)
  81.   {
  82.   cout << "\t Enter Line Number Here: ";
  83.   int choice;
  84.   cin >> choice;
  85.   cout << endl;
  86.   if (choice != 1,2,3)
  87.   {
  88.   cout << "\t You cannot follow directions. " << endl;
  89.   case 9:
  90.   cout << "\t Program will now END " << endl;
  91.   break;
  92.   }
  93.   }*/
  94. }
  95.  
  96. cout << endl;
  97.  
  98. monthlyPayment = loanAmount * (intRate[choice] / 1200) /
  99. (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));
  100.  
  101. cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl;
  102. double numPayments = length[choice] * 12;
  103. dividelist = 0;
  104.  
  105. for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
  106. {
  107. // Math Formula for intPaid to set up value for amountPaid
  108. intPaid = loanAmount * (intRate[choice] / 1200);
  109. amountPaid = monthlyPayment - intPaid;
  110. loanBalance = loanAmount - amountPaid;
  111.  
  112. if (loanBalance < 0)loanBalance = 0;
  113. loanAmount = loanBalance;
  114.  
  115. if (dividelist == 0)
  116. {
  117. cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
  118. cout << "\t\t____________" << "\t\t_____________" << endl << endl;
  119. }
  120.  
  121. cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
  122. ++dividelist;
  123.  
  124. // User to Continue, enter New Data or Quit
  125. if (dividelist == 12)
  126. {
  127. cout << endl << endl;
  128. cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
  129. cin >> listmore;
  130. if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
  131. else if ((listmore == 'N')||(listmore == 'n'))
  132. break; // Halt Current Program if New Data is entered, verify and restart
  133. else if ((listmore == 'Q')||(listmore == 'q'))
  134. return 0; // End Program
  135. }
  136. }
  137.  
  138. do // This loops valididates user input to enter New Data
  139. {
  140. cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
  141. cin >> quit;
  142. cout << "\n";
  143. }
  144.  
  145. while ((quit != 'q') &&
  146. (quit != 'Q') &&
  147. (quit != 'c') &&
  148. (quit != 'C'));
  149. }
  150.  
  151. cout << endl;
  152. system("PAUSE"); // Pause program prior to exit
  153. return EXIT_SUCCESS; // Exit
  154. }
Last edited by -_00_-; Apr 23rd, 2007 at 6:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 4
Reputation: -_00_- is an unknown quantity at this point 
Solved Threads: 0
-_00_- -_00_- is offline Offline
Newbie Poster

Re: Array and Switch issues

 
0
  #6
Apr 23rd, 2007
Even though I really did not get any help, I will post my final results of my code which includes a nicely formated output and corrected switch/case and arrays.

  1. #include <cmath> // Math Constant
  2. #include <cstdlib> // Standard Library
  3. #include <iostream> // Defines facilities for basic I/O operations
  4. #include <iomanip> // Defines facilities for basic I/O operations
  5. // cout << fixed << showpoint << setprecision(2)
  6. using namespace std; // Namespace standard library
  7.  
  8. int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
  9. {
  10. cout << " " << endl; // Empty line before assignment title
  11. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  12. cout << " // // " << endl;
  13. cout << " // Calculate and display the mortgage payment amount using the amount // " << endl;
  14. cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl;
  15. cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl;
  16. cout << " // and will select from a menu the amount of years and interest rate. // " << endl;
  17. cout << " // The mortgage payment will display and the ammortization table will // " << endl;
  18. cout << " // display the loan balance and interest paid. The user will be // " << endl;
  19. cout << " // allowed to either continue the ammortization table, enter new // " << endl;
  20. cout << " // loan data or quit the program altogether. // " << endl;
  21. cout << " // // " << endl;
  22. cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
  23. cout << " " << endl << endl << endl << endl << endl << endl;
  24.  
  25. double loanAmount, // Amount of the Loan
  26. monthlyPayment, // Monthly Payment of the Loan
  27. loanBalance, // Balance of the Loan
  28. intPaid, // Interest Paid on the Loan
  29. amountPaid, // Amount Paid on the Loan
  30. intRate[3] = {5.25,
  31. 5.50,
  32. 5.75}; // Interest Rate of the Loan
  33.  
  34. int numPayments, // Number of Payments
  35. paymentCounter,
  36. dividelist = 0, // Display partial list
  37. length[3] = {7, 15, 30}; // Length of the Loan
  38.  
  39. char listmore;
  40. char quit; // Prompt user to quit
  41. quit = 'C'; // Quit variable
  42.  
  43. while (quit != 'Q' && quit != 'q')
  44. {
  45. cout << "\t Enter Loan Amount (*NUMBERS ONLY*): ";
  46. cin >> loanAmount;
  47. cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl;
  48. cout << "\t 1. 7 Years at 5.35% " << endl;
  49. cout << "\t 2. 15 Years at 5.50% " << endl;
  50. cout << "\t 3. 30 Years at 5.75% " << endl;
  51. cout << "\t Enter Line Number Here: ";
  52.  
  53. int choice;
  54. cin >> choice;
  55. cout << endl << endl << endl;
  56.  
  57. int i;
  58. for (i = 0; i < 1; i++)
  59. switch (choice)
  60. {
  61. case 0: cout << endl;
  62. break;
  63. case 1:
  64. length[1] = 7;
  65. intRate[1] = 5.35;
  66. cout << "\t You Selected 7 Years and 5.35%" << endl;
  67. break;
  68. case 2:
  69. length[2] = 15;
  70. intRate[2]= 5.50;
  71. cout << "\t You Selected 15 Years at 5.50%" << endl;
  72. break;
  73. case 3:
  74. length[3] = 30;
  75. intRate[3] = 5.75;
  76. cout << "\t You Selected 30 Years at 5.75%" << endl;
  77. break;
  78. default:
  79. cout << "\t Invalid Line Number" << endl;
  80. }
  81.  
  82. cout << endl;
  83.  
  84. monthlyPayment = loanAmount * (intRate[choice] / 1200) /
  85. (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));
  86.  
  87. cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl;
  88. double numPayments = length[choice] * 12;
  89. dividelist = 0;
  90.  
  91. for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
  92. {
  93. // Math Formula for intPaid to set up value for amountPaid
  94. intPaid = loanAmount * (intRate[choice] / 1200);
  95. amountPaid = monthlyPayment - intPaid;
  96. loanBalance = loanAmount - amountPaid;
  97.  
  98. if (loanBalance < 0)loanBalance = 0;
  99. loanAmount = loanBalance;
  100.  
  101. if (dividelist == 0)
  102. {
  103. cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
  104. cout << "\t\t____________" << "\t\t_____________" << endl << endl;
  105. }
  106.  
  107. cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point.
  108. << "\t\t$" << loanBalance
  109. << "\t\t$" << intPaid << endl;
  110. ++dividelist;
  111.  
  112. // User to Continue, enter New Data or Quit
  113. if (dividelist == 12)
  114. {
  115. cout << endl << endl;
  116. cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
  117. cin >> listmore;
  118. if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
  119. else if ((listmore == 'N')||(listmore == 'n'))
  120. break; // Halt Current Program if New Data is entered, verify and restart
  121. else if ((listmore == 'Q')||(listmore == 'q'))
  122. return 0; // End Program
  123. }
  124. }
  125.  
  126. do // This loops valididates user input to enter New Data
  127. {
  128. cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
  129. cin >> quit;
  130. cout << "\n";
  131. }
  132.  
  133. while ((quit != 'q') &&
  134. (quit != 'Q') &&
  135. (quit != 'c') &&
  136. (quit != 'C'));
  137. }
  138.  
  139. cout << endl;
  140. system("PAUSE"); // Pause program prior to exit
  141. return EXIT_SUCCESS; // Exit
  142. }
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