| | |
Array and Switch issues
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text) - /*
- Write the program as a procedural C++ program. Allow the user to input
- the amount of a mortgage and then select from a menu of mortgage loans:
-
- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%
-
- Use an array for the different loans. Display the mortgage payment amount.
- Then, list the loan balance and interest paid for each payment over the term
- of the loan. On longer-term loans, the list will scroll off the screen.
- Do not allow the list to scroll off the screen, but rather display a partial
- list and then allow the user to continue the list. Allow the user to loop back
- and enter new data or quit. Insert comments in the program to document the
- program.
- */
-
- #include <math.h> // Math Constant
- #include <cstdlib> // Standard Library
- #include <iostream> // Defines facilities for basic I/O operations
- using namespace std; // Namespace standard library
-
- int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments
- {
- cout << " " << endl; // Empty line before assignment title
- cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
- cout << " // // " << endl;
- cout << " // Calculate and display the mortgage payment amount using the amount // " << endl;
- cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl;
- cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl;
- cout << " // and will select from a menu the amount of years and interest rate. // " << endl;
- cout << " // The mortgage payment will display and the ammortization table will // " << endl;
- cout << " // display the loan balance and interest paid. The user will be // " << endl;
- cout << " // allowed to either continue the ammortization table, enter new // " << endl;
- cout << " // loan data or quit the program altogether. // " << endl;
- cout << " // // " << endl;
- cout << " ///////////////////////////////////////////////////////////////////////// " << endl;
- cout << " " << endl << endl << endl << endl << endl << endl;
-
- double loanAmount, // Amount of the Loan
- monthlyPayment, // Monthly Payment of the Loan
- loanBalance, // Balance of the Loan
- intPaid, // Interest Paid on the Loan
- amountPaid, // Amount Paid on the Loan
- intRate[3]={5.25,
- 5.50,
- 5.75}; // Interest Rate of the Loan
-
- int numPayments, // Number of Payments
- paymentCounter,
- dividelist = 0, // Display partial list
- length[3]={7, 15, 30}; // Length of the Loan
-
- char listmore;
- char quit; // Prompt user to quit
- quit = 'C'; // Quit variable
-
- while (quit != 'Q' && quit != 'q')
- {
- cout << "\t Enter Loan Amount (*NUMBERS ONLY*): ";
- cin >> loanAmount;
- cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl;
- cout << "\t 1. 7 Years at 5.35% " << endl;
- cout << "\t 2. 15 Years at 5.50% " << endl;
- cout << "\t 3. 30 Years at 5.75% " << endl;
- cout << "\t Enter Line Number Here: ";
-
- int choice;
- cin >> choice;
- cout << endl;
-
- int i;
- for (i = 0; i < 1; i++)
- switch (choice)
- {
- case 1:
- cout << "\t You Selected 7 Years at 5.35%" << endl;
- break;
- case 2:
- cout << "\t You Selected 15 Years at 5.50%" << endl;
- break;
- case 3:
- cout << "\t You Selected 30 Years at 5.75%" << endl;
- break;
- default:
- cout << "\t Invalid Line Number" << endl;
- // Still having minor issues with invalid number
- if (choice != 1,2,3)
- {
- cout << "\t Enter Line Number Here: ";
- int choice;
- cin >> choice;
- cout << endl;
- if (choice != 1,2,3)
- {
- cout << "\t You cannot follow directions. " << endl;
- case 9:
- cout << "\t Program will now END " << endl;
- break;
- }
- }
- }
-
- cout << endl;
-
- monthlyPayment = loanAmount * (intRate[choice] / 1200) /
- (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12)));
-
- cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl;
- numPayments = length[choice] * 12;
- dividelist = 0;
-
- for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter)
- {
- // Math Formula for intPaid to set up value for amountPaid
- intPaid = loanAmount * (intRate[choice] / 1200);
- amountPaid = monthlyPayment - intPaid;
- loanBalance = loanAmount - amountPaid;
-
- if (loanBalance < 0)loanBalance = 0;
- loanAmount = loanBalance;
-
- if (dividelist == 0)
- {
- cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl;
- cout << "\t\t____________" << "\t\t_____________" << endl << endl;
- }
-
- cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl;
- ++dividelist;
-
- // User to Continue, enter New Data or Quit
- if (dividelist == 12)
- {
- cout << endl << endl;
- cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> ";
- cin >> listmore;
- if ((listmore == 'C')||(listmore == 'c'))dividelist = 0;
- else if ((listmore == 'N')||(listmore == 'n'))
- break; // Halt Current Program if New Data is entered, verify and restart
- else if ((listmore == 'Q')||(listmore == 'q'))
- return 0; // End Program
- }
- }
-
- do // This loops valididates user input to enter New Data
- {
- cout << "\tEnter 'C' to Continue, 'Q' to Quit> ";
- cin >> quit;
- cout << "\n";
- }
-
- while ((quit != 'q') &&
- (quit != 'Q') &&
- (quit != 'c') &&
- (quit != 'C'));
- }
-
- cout << endl;
- system("PAUSE"); // Pause program prior to exit
- return EXIT_SUCCESS; // Exit
- }
Last edited by -_00_-; Apr 23rd, 2007 at 3:20 pm.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
/* Write the program as a procedural C++ program. Allow the user to input the amount of a mortgage and then select from a menu of mortgage loans: 7 year at 5.35% 15 year at 5.5% 30 year at 5.75% Use an array for the different loans. Display the mortgage payment amount. Then, list the loan balance and interest paid for each payment over the term of the loan. On longer-term loans, the list will scroll off the screen. Do not allow the list to scroll off the screen, but rather display a partial list and then allow the user to continue the list. Allow the user to loop back and enter new data or quit. Insert comments in the program to document the program. */ #include <math.h> // Math Constant #include <cstdlib> // Standard Library #include <iostream> // Defines facilities for basic I/O operations using namespace std; // Namespace standard library int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments { cout << " " << endl; // Empty line before assignment title cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " // // " << endl; cout << " // Calculate and display the mortgage payment amount using the amount // " << endl; cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl; cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl; cout << " // and will select from a menu the amount of years and interest rate. // " << endl; cout << " // The mortgage payment will display and the ammortization table will // " << endl; cout << " // display the loan balance and interest paid. The user will be // " << endl; cout << " // allowed to either continue the ammortization table, enter new // " << endl; cout << " // loan data or quit the program altogether. // " << endl; cout << " // // " << endl; cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " " << endl << endl << endl << endl << endl << endl; double loanAmount, // Amount of the Loan monthlyPayment, // Monthly Payment of the Loan loanBalance, // Balance of the Loan intPaid, // Interest Paid on the Loan amountPaid, // Amount Paid on the Loan intRate[3] = {5.25, 5.50, 5.75}; // Interest Rate of the Loan int numPayments, // Number of Payments paymentCounter, dividelist = 0, // Display partial list length[3] = {7, 15, 30}; // Length of the Loan char listmore; char quit; // Prompt user to quit quit = 'C'; // Quit variable while (quit != 'Q' && quit != 'q') { cout << "\t Enter Loan Amount (*NUMBERS ONLY*): "; cin >> loanAmount; cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl; cout << "\t 1. 7 Years at 5.35% " << endl; cout << "\t 2. 15 Years at 5.50% " << endl; cout << "\t 3. 30 Years at 5.75% " << endl; cout << "\t Enter Line Number Here: "; int choice; cin >> choice; cout << endl; int i; for (i = 0; i < 1; i++) switch (choice) { case 0: cout << endl; break; case 1: cout << "\t You Selected 7 Years at 5.35%" << endl; break; case 2: cout << "\t You Selected 15 Years at 5.50%" << endl; break; case 3: cout << "\t You Selected 30 Years at 5.75%" << endl; break; default: cout << "\t Invalid Line Number" << endl; /* // Still having minor issues with invalid number if (choice != 1,2,3) { cout << "\t Enter Line Number Here: "; int choice; cin >> choice; cout << endl; if (choice != 1,2,3) { cout << "\t You cannot follow directions. " << endl; case 9: cout << "\t Program will now END " << endl; break; } }*/ } cout << endl; monthlyPayment = loanAmount * (intRate[choice] / 1200) / (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12))); cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl; double numPayments = length[choice] * 12; dividelist = 0; for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter) { // Math Formula for intPaid to set up value for amountPaid intPaid = loanAmount * (intRate[choice] / 1200); amountPaid = monthlyPayment - intPaid; loanBalance = loanAmount - amountPaid; if (loanBalance < 0)loanBalance = 0; loanAmount = loanBalance; if (dividelist == 0) { cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl; cout << "\t\t____________" << "\t\t_____________" << endl << endl; } cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl; ++dividelist; // User to Continue, enter New Data or Quit if (dividelist == 12) { cout << endl << endl; cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> "; cin >> listmore; if ((listmore == 'C')||(listmore == 'c'))dividelist = 0; else if ((listmore == 'N')||(listmore == 'n')) break; // Halt Current Program if New Data is entered, verify and restart else if ((listmore == 'Q')||(listmore == 'q')) return 0; // End Program } } do // This loops valididates user input to enter New Data { cout << "\tEnter 'C' to Continue, 'Q' to Quit> "; cin >> quit; cout << "\n"; } while ((quit != 'q') && (quit != 'Q') && (quit != 'c') && (quit != 'C')); } cout << endl; system("PAUSE"); // Pause program prior to exit return EXIT_SUCCESS; // Exit }
Last edited by -_00_-; Apr 23rd, 2007 at 6:23 pm.
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
I figured it out!!! OMG! Finally! Now to beautify the output
c++ Syntax (Toggle Plain Text)
#include <math.h> // Math Constant #include <cstdlib> // Standard Library #include <iostream> // Defines facilities for basic I/O operations using namespace std; // Namespace standard library int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments { cout << " " << endl; // Empty line before assignment title cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " // // " << endl; cout << " // Calculate and display the mortgage payment amount using the amount // " << endl; cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl; cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl; cout << " // and will select from a menu the amount of years and interest rate. // " << endl; cout << " // The mortgage payment will display and the ammortization table will // " << endl; cout << " // display the loan balance and interest paid. The user will be // " << endl; cout << " // allowed to either continue the ammortization table, enter new // " << endl; cout << " // loan data or quit the program altogether. // " << endl; cout << " // // " << endl; cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " " << endl << endl << endl << endl << endl << endl; double loanAmount, // Amount of the Loan monthlyPayment, // Monthly Payment of the Loan loanBalance, // Balance of the Loan intPaid, // Interest Paid on the Loan amountPaid, // Amount Paid on the Loan intRate[3] = {5.25, 5.50, 5.75}; // Interest Rate of the Loan int numPayments, // Number of Payments paymentCounter, dividelist = 0, // Display partial list length[3] = {7, 15, 30}; // Length of the Loan char listmore; char quit; // Prompt user to quit quit = 'C'; // Quit variable while (quit != 'Q' && quit != 'q') { cout << "\t Enter Loan Amount (*NUMBERS ONLY*): "; cin >> loanAmount; cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl; cout << "\t 1. 7 Years at 5.35% " << endl; cout << "\t 2. 15 Years at 5.50% " << endl; cout << "\t 3. 30 Years at 5.75% " << endl; cout << "\t Enter Line Number Here: "; int choice; cin >> choice; cout << endl; int i; for (i = 0; i < 1; i++) switch (choice) { case 0: cout << endl; break; case 1: length[1] = 7; intRate[1] = 5.35; cout << "\t You Selected 7 Years at 5.35%" << endl; break; case 2: length[2] = 15; intRate[2]= 5.50; cout << "\t You Selected 15 Years at 5.50%" << endl; break; case 3: length[3] = 30; intRate[3] = 5.75; cout << "\t You Selected 30 Years at 5.75%" << endl; break; default: cout << "\t Invalid Line Number" << endl; /* // Still having minor issues with invalid number if (choice != 1,2,3) { cout << "\t Enter Line Number Here: "; int choice; cin >> choice; cout << endl; if (choice != 1,2,3) { cout << "\t You cannot follow directions. " << endl; case 9: cout << "\t Program will now END " << endl; break; } }*/ } cout << endl; monthlyPayment = loanAmount * (intRate[choice] / 1200) / (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12))); cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl; double numPayments = length[choice] * 12; dividelist = 0; for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter) { // Math Formula for intPaid to set up value for amountPaid intPaid = loanAmount * (intRate[choice] / 1200); amountPaid = monthlyPayment - intPaid; loanBalance = loanAmount - amountPaid; if (loanBalance < 0)loanBalance = 0; loanAmount = loanBalance; if (dividelist == 0) { cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl; cout << "\t\t____________" << "\t\t_____________" << endl << endl; } cout << "\t\t$" << loanBalance << "\t\t\t$" << intPaid << endl; ++dividelist; // User to Continue, enter New Data or Quit if (dividelist == 12) { cout << endl << endl; cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> "; cin >> listmore; if ((listmore == 'C')||(listmore == 'c'))dividelist = 0; else if ((listmore == 'N')||(listmore == 'n')) break; // Halt Current Program if New Data is entered, verify and restart else if ((listmore == 'Q')||(listmore == 'q')) return 0; // End Program } } do // This loops valididates user input to enter New Data { cout << "\tEnter 'C' to Continue, 'Q' to Quit> "; cin >> quit; cout << "\n"; } while ((quit != 'q') && (quit != 'Q') && (quit != 'c') && (quit != 'C')); } cout << endl; system("PAUSE"); // Pause program prior to exit return EXIT_SUCCESS; // Exit }
Last edited by -_00_-; Apr 23rd, 2007 at 6:55 pm.
•
•
Join Date: Mar 2007
Posts: 4
Reputation:
Solved Threads: 0
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.
c++ Syntax (Toggle Plain Text)
#include <cmath> // Math Constant #include <cstdlib> // Standard Library #include <iostream> // Defines facilities for basic I/O operations #include <iomanip> // Defines facilities for basic I/O operations // cout << fixed << showpoint << setprecision(2) using namespace std; // Namespace standard library int main(int argc, char *argv[]) // Main, argc and *argv give the number and value of the program's command-line arguments { cout << " " << endl; // Empty line before assignment title cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " // // " << endl; cout << " // Calculate and display the mortgage payment amount using the amount // " << endl; cout << " // of the mortgage, the term of the mortgage, and the interest rate // " << endl; cout << " // of the mortgage. The user will enter the amount of the mortgage // " << endl; cout << " // and will select from a menu the amount of years and interest rate. // " << endl; cout << " // The mortgage payment will display and the ammortization table will // " << endl; cout << " // display the loan balance and interest paid. The user will be // " << endl; cout << " // allowed to either continue the ammortization table, enter new // " << endl; cout << " // loan data or quit the program altogether. // " << endl; cout << " // // " << endl; cout << " ///////////////////////////////////////////////////////////////////////// " << endl; cout << " " << endl << endl << endl << endl << endl << endl; double loanAmount, // Amount of the Loan monthlyPayment, // Monthly Payment of the Loan loanBalance, // Balance of the Loan intPaid, // Interest Paid on the Loan amountPaid, // Amount Paid on the Loan intRate[3] = {5.25, 5.50, 5.75}; // Interest Rate of the Loan int numPayments, // Number of Payments paymentCounter, dividelist = 0, // Display partial list length[3] = {7, 15, 30}; // Length of the Loan char listmore; char quit; // Prompt user to quit quit = 'C'; // Quit variable while (quit != 'Q' && quit != 'q') { cout << "\t Enter Loan Amount (*NUMBERS ONLY*): "; cin >> loanAmount; cout << "\t Select Line Number for Length of Loan and Interest Rate " << endl; cout << "\t 1. 7 Years at 5.35% " << endl; cout << "\t 2. 15 Years at 5.50% " << endl; cout << "\t 3. 30 Years at 5.75% " << endl; cout << "\t Enter Line Number Here: "; int choice; cin >> choice; cout << endl << endl << endl; int i; for (i = 0; i < 1; i++) switch (choice) { case 0: cout << endl; break; case 1: length[1] = 7; intRate[1] = 5.35; cout << "\t You Selected 7 Years and 5.35%" << endl; break; case 2: length[2] = 15; intRate[2]= 5.50; cout << "\t You Selected 15 Years at 5.50%" << endl; break; case 3: length[3] = 30; intRate[3] = 5.75; cout << "\t You Selected 30 Years at 5.75%" << endl; break; default: cout << "\t Invalid Line Number" << endl; } cout << endl; monthlyPayment = loanAmount * (intRate[choice] / 1200) / (1 - pow(1 + (intRate[choice] / 1200), - 1 * (length[choice] * 12))); cout << "\t Monthly Payments: $" << monthlyPayment << endl << endl; double numPayments = length[choice] * 12; dividelist = 0; for (paymentCounter = 1; paymentCounter <= numPayments; ++paymentCounter) { // Math Formula for intPaid to set up value for amountPaid intPaid = loanAmount * (intRate[choice] / 1200); amountPaid = monthlyPayment - intPaid; loanBalance = loanAmount - amountPaid; if (loanBalance < 0)loanBalance = 0; loanAmount = loanBalance; if (dividelist == 0) { cout << "\t\tLoan Balance" << "\t\tInterest Paid" << endl; cout << "\t\t____________" << "\t\t_____________" << endl << endl; } cout << fixed << showpoint << setprecision(2) // Sets the number of digits printed to the right of the decimal point. << "\t\t$" << loanBalance << "\t\t$" << intPaid << endl; ++dividelist; // User to Continue, enter New Data or Quit if (dividelist == 12) { cout << endl << endl; cout << "\tEnter 'C' to Continue, " << "'N' for New Data, " << "'Q' to Quit> "; cin >> listmore; if ((listmore == 'C')||(listmore == 'c'))dividelist = 0; else if ((listmore == 'N')||(listmore == 'n')) break; // Halt Current Program if New Data is entered, verify and restart else if ((listmore == 'Q')||(listmore == 'q')) return 0; // End Program } } do // This loops valididates user input to enter New Data { cout << "\tEnter 'C' to Continue, 'Q' to Quit> "; cin >> quit; cout << "\n"; } while ((quit != 'q') && (quit != 'Q') && (quit != 'c') && (quit != 'C')); } cout << endl; system("PAUSE"); // Pause program prior to exit return EXIT_SUCCESS; // Exit }
![]() |
Similar Threads
- strings as input, substr and len (C++)
- PATA raid 0 issues in bios (Storage)
- New iMac vs New PC - A purchase choice? (IT Professionals' Lounge)
- Declaring string array, initializing later... (C++)
Other Threads in the C++ Forum
- Previous Thread: Getline with multiple delimiters??
- Next Thread: passing by reference a 2D array in C++
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete deploy desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






