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!! - #include <iostream>
-
#include <iomanip>
-
#include <cmath>
-
#include <cctype>
-
-
-
using namespace std;
-
int main()
-
-
{
-
double a; //This is the amount of the mortgage the user must enter
-
double i; //This is the interest rate the user must enter
-
int y; //This is the amount of years for the mortgage the user must enter
-
double mPayment; //This is a variable for ouputting the payment
-
char YesNo = 'Y';
-
-
-
do //In order to allow the user to be able to re-enter date, we must have a loop
-
-
{
-
cout << "What is the amount of the mortgage? For example 200000" << endl;
-
cout << "Press enter." << endl;
-
cin >> a;
-
cout << endl;
-
cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
-
cout << "Press enter." << endl;
-
cin >> y;
-
cout << endl;
-
cout << "What is the interest rate? For example 5.75" << endl;
-
cout << "Press enter." << endl;
-
cin >> i;
-
cout << endl;
-
-
//These are the variables required to calculate the information the user inputs
-
double monInterest = i / 12 / 100; //Calcualtes the interest monthly
-
int t = y * 12; //This is the loan term in the amount of months
-
-
//This is the actual formula for calculating the mortgage payment amount
-
mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
-
-
//This allows the user to view what they entered and also what the monthly payment would be
-
cout << "Amount of mortgage = $" << a << endl;
-
cout << "Year financed = " << i << "%" << endl;
-
cout << "Interest Rate = " << y << " years" << endl;
-
cout << endl;
-
cout << "Monthly Payment Amount = $" << mPayment << endl;
-
cout << endl;
-
-
-
//This allows the user to either enter in new information, or exit the program
-
cout << "If you would like to enter different information, please press Yes." << endl;
-
cout << "If you would like to exit this program, please press No." << endl;
-
cout << endl;
-
cin >> YesNo;
-
}
-
-
while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop
-
-
return 0;
-
-
}
-
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
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.
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.
This is the code I have so far.....any help would be most appreciated!!! - #include <iostream>
-
#include <iomanip>
-
#include <cmath>
-
#include <cctype>
-
-
-
using namespace std;
-
int main()
-
-
{
-
double a; //This is the amount of the mortgage the user must enter
-
double i; //This is the interest rate the user must enter
-
int y; //This is the amount of years for the mortgage the user must enter
-
double mPayment; //This is a variable for ouputting the payment
-
char YesNo = 'Y';
-
int b;
-
-
do //In order to allow the user to be able to re-enter date, we must have a loop
-
-
{
-
cout << "What is the amount of the mortgage? For example 200000" << endl;
-
cout << "Press enter." << endl;
-
cin >> a ,b;
-
if(isdigit(b))
-
{
-
cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
-
cout << "Press enter." << endl;
-
}
-
else
-
{
-
cout<<"You have not entered a valid entry! Please try again.";
-
}
-
}while ((YesNo == 'Y') || (YesNo == 'y'));
-
cout << endl;
-
cout << "What is the amount of years the mortgage will be financed? For example 30" << endl;
-
cout << "Press enter." << endl;
-
cin >> y;
-
if(isdigit(b))
-
{
-
cout << endl;
-
cout << "What is the interest rate? For example 5.75" << endl;
-
cout << "Press enter." << endl;
-
cin >> i;
-
cout << endl;
-
-
//These are the variables required to calculate the information the user inputs
-
double monInterest = i / 12 / 100; //Calcualtes the interest monthly
-
int t = y * 12; //This is the loan term in the amount of months
-
-
//This is the actual formula for calculating the mortgage payment amount
-
mPayment = (a * monInterest) / (1-pow((1+monInterest),-t));
-
-
//This allows the user to view what they entered and also what the monthly payment would be
-
cout << "Amount of mortgage = $" << a << endl;
-
cout << "Year financed = " << i << "%" << endl;
-
cout << "Interest Rate = " << y << " years" << endl;
-
cout << endl;
-
cout << "Monthly Payment Amount = $" << mPayment << endl;
-
cout << endl;
-
-
-
//This allows the user to either enter in new information, or exit the program
-
cout << "If you would like to enter different information, please press Yes." << endl;
-
cout << "If you would like to exit this program, please press No." << endl;
-
cout << endl;
-
cin >> YesNo;
-
}
-
-
while ((YesNo == 'Y') || (YesNo == 'y')); //End of the loop
-
-
return 0;
-
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
...
|
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 =...
|
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...
|
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...
|
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...
|
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
*
...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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,...
|
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...
| |