473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trapping Bad Entries in a C++ Mortgage calculator program

The problem is that my code below used to run wonderfully, until the instructor decided that he wants to use characters instead of integers, and wants my code to trap the bad and have the program state that the user did not imput the correct values. Can someone help me figure out how to make my code work that way??? I have the majority of the code done.....but....I still need that last step!! Thanks!!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cctype>        
  5.  
  6.  
  7. using namespace std;
  8. int main()
  9.  
  10. {
  11.      double a; //This is the amount of the mortgage the user must enter
  12.      double i; //This is the interest rate the user must enter
  13.      int y; //This is the amount of years for the mortgage the user must enter            
  14.      double mPayment; //This is a variable for ouputting the payment            
  15.      char YesNo = 'Y';
  16.  
  17.  
  18. do //In order to allow the user to be able to re-enter date, we must have a loop
  19.  
  20. {
  21.     cout << "What is the amount of the mortgage?  For example 200000" << endl;
  22.     cout << "Press enter." << endl;
  23.     cin    >>    a;    
  24.     cout << endl;
  25.     cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
  26.     cout << "Press enter." << endl;
  27.     cin >>    y;
  28.     cout << endl;
  29.     cout << "What is the interest rate? For example 5.75" << endl;
  30.     cout << "Press enter." << endl;
  31.     cin >>    i;
  32.     cout << endl;
  33.  
  34. //These are the variables required to calculate the information the user inputs
  35. double monInterest = i / 12 / 100; //Calcualtes the interest monthly        
  36. int t = y * 12; //This is the loan term in the amount of months
  37.  
  38. //This is the actual formula for calculating the mortgage payment amount 
  39. mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
  40.  
  41. //This allows the user to view what they entered and also what the monthly payment would be
  42.     cout << "Amount of mortgage = $" << a << endl;
  43.     cout << "Year financed = " << i << "%" << endl;
  44.     cout << "Interest Rate = " << y << " years" << endl;
  45.     cout << endl;
  46.     cout << "Monthly Payment Amount = $" << mPayment << endl;
  47.     cout << endl;
  48.  
  49.  
  50. //This allows the user to either enter in new information, or exit the program
  51.     cout << "If you would like to enter different information, please press Yes." << endl;
  52.     cout << "If you would like to exit this program, please press No." << endl;
  53.     cout << endl;
  54.     cin >> YesNo;
  55. }
  56.  
  57. while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop    
  58.  
  59.     return 0;
  60.  
  61. }
  62.  
Sep 8 '06 #1
4 6961
Banfa
9,065 Expert Mod 8TB
It's not entirely clear what you mean, are you talking about making the variables y and t type char instead of type int?

While that shouldn't be too much of a problem for y (unlikely to get terms > 255 years) it may be for t which is y * 12.

To ctach the bad case you need to see if y * 12 will be greater than 255

y * 12 > 255

That is actually not possible without breaking the constraints of a char but you can manipulate the equation so that a valid check is made.

Just divide both sides of the equation by 12 (simple algabra)

y * 12 / 12 > 255 / 12

simplying

y > 21.25

since it's integer arithmatic drop the fraction

y > 21

is the error condition
Sep 8 '06 #2
Ok, maybe I was not making myself clear. Attached is exactly what my instructor said....that might help....

"Modify the mortgage program to input the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Display the mortgage payment amount. Allow the user to loop back and enter new data or quit. Insert comments in the program to document the program. This program should still be a procedural C++ program. The first version of the code is used to allow students to work on the process of writing the code, getting it reviewed, and applying feedback. In the subsequent versions, we add features to the initial version. While accepting input may be necessary to perform a calculation, the emphasis is on error checking the user input. This is not a trivial task.. Also, error checking will require the use of looping and arrays, which we recently addressed and will continue this week.

btw. the values I use to test the input amount are : 123.45 123,45 1a2b.a4 123.4a and a234.56
Your code should identify the bad input, notify the user, and take appropriate action."

I hope this explains better what I am trying to do, but am stuck with. I was thinking I could use isdigit(), but I think that is only used for single integers.
Sep 9 '06 #3
I have being trying to figure out how I can validate my code using isdigit(), but I am not sure if I can use isdigit() when there is more than one number. I was also told to try islanum(), but I have never heard of this and unsure what it is used for.
Sep 9 '06 #4
This is the code I have so far.....any help would be most appreciated!!!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>        
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cctype>        
  5.  
  6.  
  7. using namespace std;
  8. int main()
  9.  
  10. {
  11.      double a; //This is the amount of the mortgage the user must enter
  12.      double i; //This is the interest rate the user must enter
  13.      int y; //This is the amount of years for the mortgage the user must enter            
  14.      double mPayment; //This is a variable for ouputting the payment            
  15.      char YesNo = 'Y';
  16.      int b;
  17.  
  18. do //In order to allow the user to be able to re-enter date, we must have a loop
  19.  
  20. {
  21.     cout << "What is the amount of the mortgage?  For example 200000" << endl;
  22.     cout << "Press enter." << endl;
  23.     cin    >>    a ,b;
  24.     if(isdigit(b))
  25.     {
  26.     cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
  27.     cout << "Press enter." << endl;
  28.     }
  29.     else
  30.     {
  31.     cout<<"You have not entered a valid entry! Please try again.";
  32.     }
  33. }while ((YesNo == 'Y') || (YesNo == 'y'));   
  34.     cout << endl;
  35.     cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
  36.     cout << "Press enter." << endl;
  37.     cin >>    y;
  38.     if(isdigit(b))
  39.     {
  40.     cout << endl;
  41.     cout << "What is the interest rate? For example 5.75" << endl;
  42.     cout << "Press enter." << endl;
  43.     cin >>    i;
  44.     cout << endl;
  45.  
  46. //These are the variables required to calculate the information the user inputs
  47. double monInterest = i / 12 / 100; //Calcualtes the interest monthly        
  48. int t = y * 12; //This is the loan term in the amount of months
  49.  
  50. //This is the actual formula for calculating the mortgage payment amount 
  51. mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
  52.  
  53. //This allows the user to view what they entered and also what the monthly payment would be
  54.     cout << "Amount of mortgage = $" << a << endl;
  55.     cout << "Year financed = " << i << "%" << endl;
  56.     cout << "Interest Rate = " << y << " years" << endl;
  57.     cout << endl;
  58.     cout << "Monthly Payment Amount = $" << mPayment << endl;
  59.     cout << endl;
  60.  
  61.  
  62. //This allows the user to either enter in new information, or exit the program
  63.     cout << "If you would like to enter different information, please press Yes." << endl;
  64.     cout << "If you would like to exit this program, please press No." << endl;
  65.     cout << endl;
  66.     cin >> YesNo;
  67. }
  68.  
  69. while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop    
  70.  
  71.     return 0;
  72.  
  73. }
  74.  
Sep 9 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
0
by: JohnJohnUSA | last post by:
I ran the following program to retrieve entries from the windows registry on Windows XP: import win32api, win32con aReg = win32api.RegConnectRegistry(None, win32con.HKEY_CURRENT_USER) aKey =...
3
by: promiscuoustx | last post by:
I am trying to get my program to compile, but it will not complete. At line 79 it states, cannot convert 'float()()' to 'float' in assignment. Here is my code. #include <iostream> #include...
3
by: cameron | last post by:
Hi I am new here in this forum: I am writing a C++ program to calculate a Montly Mortgage Payment where the loan amount is 200,000.00 with a 5.75% interest rate with a term of 30 years. My program...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
1
by: phjones | last post by:
please help resolve some error messages code is compling with errors see below the code. I am new at this please help! /** * @(#)3 Mortgage loans.java * * 3 Mortgage loans application * ...
1
by: phjones | last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the...
1
by: dylbin | last post by:
I am having trouble with the following program: Without using a G.U.I., using a loan amount of $200,000 with an interest rate of 5.75% and a 30 year term, I have to display the mortgage payment...
3
by: zaidalin79 | last post by:
I have finally gotten my GUI to look like I want it to, but I am having trouble getting the calculations right. No matter what I put in there, it seems to calculate a large payment, and a very wrong...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.