473,782 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trapping Bad Entries in a C++ Mortgage calculator program

11 New Member
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 6984
Banfa
9,065 Recognized Expert Moderator Expert
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
promiscuoustx
11 New Member
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
promiscuoustx
11 New Member
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
promiscuoustx
11 New Member
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
7306
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.... #include <stdio.h> #include <stdlib.h>
0
1532
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 = win32api.RegOpenKeyEx(aReg, r"Software\Microsoft\Internet Explorer\PageSetup") for i in range(100):
3
4037
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 <iomanip> #include <cmath> #include <cctype> #include <stdlib.h> #include <sstream> using namespace std;
3
6702
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 compiles fine with no errors but my calculation is not working correctly, any suggestions would be great as this assignment is due on Monday: //******************************************************* //Program: Calculations Payments //Purpose: To...
2
7892
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 path, import java.math.*;//*loan calculator import java.text.*;//*formats numbers public class 3 Mortgage loans { // declare class variable array int mortgage calculator
1
9823
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 * * Phyllis J Jones * Purpose to write a program in Java without a graphical user interface and have
1
6843
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 following error message: --------------------Configuration: <Default>-------------------- C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\mortgageloan\src\mortgageloan.java:30: operator > cannot be applied to double,int ...
1
2212
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 amount and then list the loan balance and interest paid for each payment over the term of the loan.
3
3619
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 amortization schedule... Here is what I have so far... package guiweek3; //imports necessary tools import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*;
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.