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 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
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....
#include <stdio.h>
#include <stdlib.h>
|
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):
|
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;
|
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...
|
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
| |
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
|
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
...
|
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.
|
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.*;
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |