C/C++ :: How To Incorporate Exception Handling Code Into Calculate Mortgage Program

Dec 15, 2014

How to incorporate exception handling code into my existing calcMortgage code. While I was researching exception handling, I thought "what would happen with my current code if someone input the principal with a comma in it?". Typically people write two hundred thousand like so.... 200,000. While experimenting with my original code, I remembered reading in my research that someone had done their calcMortgage with the output prompt "DO NOT USE COMMAS". So, when checking to see if my code would run, I did not use commas.

Well, guess what...using a comma in the principal causes an error with a negative numerical output. lol PERFECT!!!! Obviously, the easy thing to do would be to put output instructions in the code telling the user NOT to use commas, but the assignment requires me to use exception handling. The code itself works, but the calculation produces a negative monthly payment.

How would I insert exception handling code into my current code to correct this problem??

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
struct calcMortgage {
double Principal, numYears, IntRate, monthlyPayments;};
int main(){

[Code]...

would I use a try or a throw??

View 14 Replies


ADVERTISEMENT

C/C++ :: Exception Handling Program

Feb 17, 2015

write a program as described below: program that reads in two integers (age, social security number). You should write functions that throw an out-of-range exception forage (no negative numbers)SSN (must be a 9-digit integer) My code is written below:

#include "std_lib_facilities_4.h"
int main(){
int age = 0;
int ssn = 0;

[Code].....

View 8 Replies View Related

C++ :: Writing A Program That Requires Exception Handling

Nov 24, 2013

writing a program that requires exception handling. if an error occurs, i what the program to go back to the begging of the loop. i tried using break but that just makes the program crash when it receives a bad input. how do i do this? this is what i have so far (this part of the program any ways)

while (! quit)
{
// Output phone book menu
cout << endl

[Code].....

View 1 Replies View Related

C++ :: How Exception Handling Works

Oct 25, 2014

I know how exception handling works, but how should I actually use it in action? Let's say I have something like this:

Class X
{
public:
X();
//Pre-condition: example can't be 3.
void number(int example);

[code]......

If I know that the exception can only happen at the beginning of the function, is it okay to catch it right away?

View 7 Replies View Related

C++ :: Handling More Than One Exception At A Time

Feb 12, 2015

It is advisable not to throw the exception from destructor. Because if exception happens stack unwinding happens. suppose the destructor again throws the exception then on part of first exception again one exception is thrown and exceptions can not be handled at same time. This is what i read from stack over flow.

View 10 Replies View Related

C++ :: Exception Handling Object?

Jun 9, 2012

I'm out of track how to realize one point in exercise condition wording :

Give the OutOfBoundsException class a constructor with an int as argument that indicates the erroneous array index and store it in a data member.

How should I change the code below .

Code:
//array.h
#ifndef Array_H
#define Array_H
#include "point.h"
#include <string>
using namespace std;
class Array {
private:
Point* m_data; // Dynamic Array of Point pointers

[code]....

View 5 Replies View Related

C++ :: Operator Overloading And Exception Handling

Nov 15, 2013

I have a date class and i overloaded operator >> to accept input in dd/mm/yyyy format. if i enter the wrong date format my program will crash. How do i do exception handling for this? How should i do the try part? and for catch, I'll just catch a date class variable?

Code:
void operator >> (istream &is, clsDate &date) {
string inputDate;
is >> inputDate;
int mm = stringToNumber(inputDate.substr(3,2)); // read 2 characters from character number 3 start
int dd = stringToNumber(inputDate.substr(0,2)); // read 2 characters from character number 0 start
int yy = stringToNumber(inputDate.substr(6,4)); // read 4 characters from character number 6 start

[Code] .....

View 2 Replies View Related

C/C++ :: Postfix Calculator Using Stack With Exception Handling?

Mar 15, 2015

I'm having some significant trouble with an assignment to create a postfix calculator that simulates the dc calculator function in linux. I have attached the handout with project instructions, but my main problem at the moment lies with parsing through the string input and adding the numbers into a stack of ints.

 Project #2.pdf (47.78K)

Here's a brief summary of the methods used in the switch statement:

OPERATORS AND COMMMAND INPUTS
+ : Pops two values off the stack, adds them, and pushes the result.
- : Pops two values, subtracts the first one popped from the second one popped, and pushes the result.
* : Pops two values, multiplies them, and pushes the result.
/ : Pops two values, divides the second one popped from the first one popped, and pushes the result.
% : Pops two values, computes the remainder of the division that the / command would do, and pushes that.

Commands

p - Prints the value on the top of the stack, without altering the stack. A newline is printed after the value.

f - Prints the entire contents of the stack without altering anything. A newline is printed after each value

n - Prints the value on the top of the stack, pops it off, and does not print a newline after.

c - Clears the stack, rendering it empty.

d - Duplicates the value on the top of the stack, pushing another copy of it. Thus "4d*p" computes 4 squared and prints it.

r - Reverses the order of (swaps) the top two values on the stack.

Exception handling also needs to be added to account for division by zero and and invalid operator.

Right now my biggest problem is that I keep getting the following strange output where a 0 is automatically added to the stack when I call a function or operator. I realize this is probably because of the lines I have placed outside of the for loop that read

//END FOR LOOP
int num = atoi(operands.c_str());
myStack.push(num);
operands = "";
cout << num << " added." << endl;

But when I tried putting these statements INSIDE the for loop, I just get more errors. I've been working on this for a number of hours but I can't figure out my issue. I've also attached an image of my current output.

#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;
#include "stack.h"
bool isOperator(const char& input );

[code]....

View 6 Replies View Related

Visual C++ :: Unable To Find Setting For Exception Handling In MFC

Jan 24, 2013

My MDI Project(VC++2010 Professional) is unable to catch errors ,though I return ,try catch block. So I developed simple dialog based application .Placed one button on Dialog and on its click written following code

Collapse | Copy Code
void CMFCExecDlg::OnBnClickedButton1() {
try {
int j = 0;
int i = 10/j;
}
catch(CException * e) {
MessageBox(_T("Hello"),_T(""),MB_OK);
}
}

But still program control does not come in catch block it simply gives error. I tried all child classes of CException but no use.I think there will be some setting in Visual Studio. How to handle exceptions

View 1 Replies View Related

C++ :: Templates (composition) Exception Handling Run-time Error

Aug 4, 2012

The code is compiled.

If I do loops for push and pop fucntions in main.cpp the same size as Stoke everything is working properly

If loops are larger than the Stack size that goes here is a picture in the console (see screen print)

Code:
//
//(---.Array_hpp---)
//
#ifndef Array_HPP// Preprocessor gates
#define Array_HPP
#include <sstream>
#include <iostream>
#include <exception>
template <class Type>//Remove the "=double" default parameter.

[code]....

View 4 Replies View Related

C :: Incorporate Command Line Arguments Into Program?

May 26, 2014

In the following program, I have gotten it to work with IO redirection (not shown in this particular code), and have made some minor adjustments to get it to work with file processing. The only thing I have to do now is get it to work with command line arguments, and am slightly lost (and I have looked into it).

I basically just need to take a text file from a certain directory and send it to my program. Would need two files, an input and output file.

main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LAB_6B_7B.h"

[Code].....

View 8 Replies View Related

C++ :: Incorporate Button Into Current Program That Opens Up Calculator?

Nov 10, 2013

I want to incorporate a button into my current program that opens up a calculator in a different window. I have the code for the calculator and the program. I have never worked with buttons or windows.

View 4 Replies View Related

C++ :: How To Implement Discounts - Calculate Shipping / Handling

Oct 3, 2013

How to implement the discounts and the shipping and handling

The store sells chocolates:
•Milk Chocolate @ $8.50 per pound
•Dark European Chocolate @ $9.75 per pound
•White Chocolate @ $10.50 per pound
•European Truffles @ $12.50 per pound

The store allows a customer discount based on:
1.$20.00 to $39.99 10% off
2.$40.00 to $59.9915% off
3.$60.00 to $79.9920% off
4.$80.00 and over25% off

Shipping & Handling is 10% of the net total (gross – discount).
Total Owed is Net Total plus Shipping and Handling)

You are using the switch to calculate the quantity discount. Since the switch does not allow a range, you have to set a “code” for the quantity range to use in the Switch such as and integer code 1, 2, 3, or 4 or maybe a char ‘a’, ‘b’, ‘c’ or ‘d’.

#include <iostream>
#include <conio.h>
using namespace std;
int main () {
int choice; // Hold a menu of choice
int pounds; // Hold the number of pounds
double charges; // Hold the price range
float discount; //Hold discount range

[Code] .....

View 3 Replies View Related

C++ :: Mortgage Calculator - Displaying Months In Correct Order

Mar 29, 2013

I need my code to display Month but it seems my code is skipping month's and not displaying 1, 2 3. It's going 1 , 3 ,5 and so on. This is the code i have for it

Code:
int month = 1;

And later on the code i have month++ That's my first mistake and i'm not sure how to possibly fix it. My second mistake is my Interest Paid output. This is the code i have for it

Code:
interestPaid = loanBalance*i;

This is on the while loop, but i'm not sure why it's not displaying the correct amount. It should display for month 1 = 500.00 , Month 2 = 498.28 and Month 3 = 496.55 and so on... However my code display's something close to it, but not that. i is my monthly interest rate. My code for it seems to be fine. If i can fix this two things i'll be able to finish the project. This is my entire code, how i can fix it so that my month is displayed in 1, 2, 3, etc.... and not 1, 3, 5, etc... And why my interest is not displaying what my professor has on his output screen (i provided the first three months on top in bold) If i can fix those things i'll have the rest. Here's my entire code

#include <iostream>
#include <iomanip>
using namespace std;
int main ( ) {
int month = 1;
// Holds the user's amount of loan.
double loanAmount;
// Holds the user's interest rate.
double i;

[Code] ....

View 3 Replies View Related

C :: Write A Code To Calculate Minimum Number

May 24, 2014

I'm trying to write a code to calculate the minimum number of coins needed to cover a change. It works fine but i want to display a message telling the user to try again if they type something that's not a number or a negative number. But it's not working. When i type a negative number, the program runs normally and displays a negative value of coins instead of showing the message. And when i type something that's not a number, the program just keeps showing a lot of zeros nonstop. And when i try to compile it with 'unsigned float' instead of just 'float' i get errors.

Code:

#include <stdio.h>
int main()
{
unsigned int coins;
float change;
unsigned int n25, n10, n5, n1;
unsigned int r25, r10, r5;
}

[code]....

View 7 Replies View Related

C/C++ :: General Protection Exception Is Written When Execute Program

May 8, 2012

My code in c++ is

#include<iostream.h>
main()
{int a[10],x[5];
a[-7]=15;
x[5]=20;
cout<<x[-5];
}

View 2 Replies View Related

C :: Program To Execute Foreground Process - Signal Handling

Feb 3, 2014

I am trying to create a program that would execute a foreground process. I have this code for the foreground process

Code:
#include <stdio.h>
#include <signal.h>
int main() {
int temp;
printf("We are in the current program

[Code] ....

And this is my main program

Code:
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
int main() {
signal(SIGINT, SIG_IGN);

[Code] ....

My main program stops executing after the process "./scanf" executes and there fore does not print the line "How are You"...

I want to main program to continue executing after "./scanf" finishes. How to achieve that?

View 2 Replies View Related

C/C++ :: Quiz Program Based On Data File Handling

Dec 22, 2014

I'm trying to make a Quiz program based on C++'s Data File Handling capablities. I've attached the code in .txt file and I wonder why I'm getting the error identifier bScience cannot have a type qualifier?

Attached Files QuizPro.txt (9.6 KB)

View 7 Replies View Related

C++ :: Incorporate Array Of Structures To Read User Input Then Reprint That Information To Screen

Nov 18, 2014

I have to create a program that incorporates an array of structures in order to simply read input from the user and then reprint that information to the screen. This is a fairly simple problem, but the issue is that the professor requires that we solve it in a very specific way.

I must have three functions in total: one to receive input from the user, one to print the received values, and of course the main function which calls the other two. Also, the input and output functions must be defined using the following prototypes:

void inputData(Component C1);
void printData(Component *C1);

Where Component is a structure defined above as:

typedef struct// Component {
float cost;
int code;
char name[30];

[Code] ....

I know that this problem could be easily solved by simply passing the entire 'comp' array to the inputData function, or passing a pointer to it, or having it return a pointer, but I am not allowed to do any of these things. I must use the function as defined by the prototype.

View 3 Replies View Related

C++ :: File Handling - Program To Read Inputs In TXT File

Mar 26, 2013

I've written a program to read inputs in a text file. The file named Input.txt contain integers 2 3 and 4. My program is as follows:

#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
int main (){
int x,y,z;

[Code] ....

the program is outputing x=2293616 , y=0 and z=0 instead of x=2, y=3 and z=4.

View 9 Replies View Related

C++ :: Program To Calculate BMI

Oct 7, 2013

I'm trying to successfully run a program that calculates your BMI but, although it runs, it is not giving the the correct math. Instead, it gives me the number i've submitted as my weight as an answer. I'm using Visual Studio 2008 and the formula: weight / (height/100)*2

Here is my code

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int weight;
int height;
double BMI;

[Code] ....

View 2 Replies View Related

C++ :: Program To Calculate Fractions?

Oct 24, 2013

I need to write a program that can calculate fractions.

View 2 Replies View Related

C++ :: Program That Calculate Discounts

Nov 8, 2013

So my midterm exam for programming is done. My program didn't run. Tried rewriting it once I got home. I am still getting an error that the program could not be run. Also, we weren't allowed to use the internet. So I didn't know how to calculate for the discounted rate.

I am using Microsoft Visual Studio 2010.

Given problem:

ABC Hotel offers 5-10% discounts depending on how much money has the user spent. If item purchase is lesser than 1000php (1USD is 43.19PHP) 5% discount is availed. Greater than 1000php then 10% discount is availed. Total price and discounted price should be displayed.

#include<iostream>
using namespace std;
main() {
float a,b,c,d,e,f,g,h,i,j,total,discounted_price;
cout<<"Please input price of first 1st item: ";

[Code] .....

View 1 Replies View Related

C :: Calculate Offset Of Simple Program

Sep 28, 2013

I would to learn how could i calculate the offset of simple c program?

Lets say that I have that simple program:

Code:
#include <stdio.h>
int main() {
int x=1;
printf("x = %d", x);
return 0;
}

View 9 Replies View Related

C :: How To Write A Program That Will Calculate Stress

Feb 7, 2013

I recently signed up for ES201 class and am confused with how to compile a program in the C language that will convert ACT scores to SAT scores.

View 5 Replies View Related

C :: Program That Will Calculate Strain Of Object

Feb 7, 2013

The equation for strain is:

delta(x)/x_0
delta(x) is the change in length of the rod(final length - initial length) x_0 is the original length of the rod

I'm trying to figure out how to write a program that will calculate the strain of an object, given its initial length and final length.

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved