Chapter 9 Completing the Basics

7
Chapter 9 Completing the Basics C++ for Engineers and Scientists Third Edition

description

C++ for Engineers and Scientists Third Edition. Chapter 9 Completing the Basics. Run Time E rrors. #include using namespace std; int main() { int top, bottom; cout > top; - PowerPoint PPT Presentation

Transcript of Chapter 9 Completing the Basics

Page 1: Chapter 9 Completing the Basics

Chapter 9Completing the Basics

C++ for Engineers and Scientists

Third Edition

Page 2: Chapter 9 Completing the Basics

#include <iostream>using namespace std;

int main(){ int top, bottom;

cout << "Enter the top number (whole number only): "; cin >> top; cout << "Enter the bottom number (whole number only): "; cin >> bottom;

cout << top <<'/' << bottom << " = " << double(top)/ double(bottom) << endl;}

Run Time ErrorsRun Time Errors

Page 3: Chapter 9 Completing the Basics

Catch the errorCatch the error

#include <iostream>using namespace std;

int main(){ int numerator, denominator; try { cout << "Enter the numerator (whole number only): "; cin >> numerator; cout << "Enter the denominator(whole number only): "; cin >> denominator;

if (denominator == 0) throw denominator; // an integer value is thrown else cout << numerator <<'/' << denominator

<< " = " << double(numerator)/ double(denominator) << endl; } catch(int e) { cout << "A denominator value of " << e << " is invalid." << endl; exit (1); // Abnormal exit } }

Page 4: Chapter 9 Completing the Basics

Exception Handling

• General syntax of code required to throw and catch and exception:

• catch an exception

Page 5: Chapter 9 Completing the Basics

Exception Handling

Page 6: Chapter 9 Completing the Basics

P. 518, Ex. 6

Send program and outputs To: [email protected] Subject: 32110236, CS125, 2013.5.7

Page 7: Chapter 9 Completing the Basics

P. 518, Ex. 7 Modify the program so that it continues to

try to divide 2 numbers until the user enters the character q to quit.

Send program and outputs To: [email protected] Subject: 32110236, CS125, 2013.5.7