Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program.

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program."— Presentation transcript:

1 Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program

2 Today We Are Going To: Demonstrate the use of constants Demonstrate the use of constants Discuss the importance of programming style for preventing and resolving errors Discuss the importance of programming style for preventing and resolving errors Work with integer arithmetic Work with integer arithmetic Introduce the concept of type casting Introduce the concept of type casting

3 Topic Constants

4 Constants Used these last time Used these last time Values that do not change Values that do not change Giving symbolic names makes program easier to write, to change, and to understand Giving symbolic names makes program easier to write, to change, and to understand Declaration: ordinary variable with addition of the keyword const Declaration: ordinary variable with addition of the keyword const

5 Topic Errors and Debugging

6 Syntax Errors These are the easiest errors to find and fix; compiler finds them and gives a hint These are the easiest errors to find and fix; compiler finds them and gives a hint Often one mistake produces several errors - “cascading” errors - so fix first ones first Often one mistake produces several errors - “cascading” errors - so fix first ones first Error message will help, but you should also know yourself, know what mistakes you have a tendency to make Error message will help, but you should also know yourself, know what mistakes you have a tendency to make

7 Runtime Errors Cause abnormal termination of program Cause abnormal termination of program The runtime environment will give some information about the exception The runtime environment will give some information about the exception Harder to track down because the place where the error shows up is not always the same as the place that caused it Harder to track down because the place where the error shows up is not always the same as the place that caused it

8 Logic Errors Program runs normally but gives the wrong result Program runs normally but gives the wrong result These are very hard to find; you must know the calculation you are trying to perform very well These are very hard to find; you must know the calculation you are trying to perform very well

9 Error Prevention Follow good programming practices Follow good programming practices Use naming conventions Use naming conventions Indent properly Indent properly Comment clearly (esp. for logic errors) Comment clearly (esp. for logic errors) Write code in small steps, test each one Write code in small steps, test each one Use the compiler often (Recall: Why does C++ make use of a compiler?) Use the compiler often (Recall: Why does C++ make use of a compiler?)

10 Error Detection Compiler errors are easy to detect: the compiler tells you about them Compiler errors are easy to detect: the compiler tells you about them Run-time errors and logic errors require that the program be tested Run-time errors and logic errors require that the program be tested Develop a broad set of test cases Develop a broad set of test cases Make sure to include potential special cases Make sure to include potential special cases

11 Error Correction Most code-generation applications include a debugging tool Most code-generation applications include a debugging tool Use this to stop execution at a place that is suspicious Use this to stop execution at a place that is suspicious Once stopped you can step through the instructions one at a time Once stopped you can step through the instructions one at a time Displays variables and other information to help identify the source of the problem Displays variables and other information to help identify the source of the problem

12 Topic Working From the Command Line

13 DOS Prompts Find a DOS prompt: Start/Accessories Find a DOS prompt: Start/Accessories Navigate to the location of your executable Navigate to the location of your executable Execute it by invoking it as a command Execute it by invoking it as a command Watch installation of Borland compiler Watch installation of Borland compiler

14 Topic Integer Arithmetic and An Introduction to Type Casting

15 Project 2.2 Open your book Open your book Read Project 2.2 Read Project 2.2 Discuss the program’s requirements Discuss the program’s requirements Analyze input and output Analyze input and output Design a solution Design a solution

16 Quotient Calculation Notice the way the quotient is truncated Notice the way the quotient is truncated Integer division must produce an integer result; the fractional part is LOST Integer division must produce an integer result; the fractional part is LOST Recall: division and modulus operators for integer data Recall: division and modulus operators for integer data

17 Type Casting In order to overcome this problem we must tell the compiler NOT to do integer division In order to overcome this problem we must tell the compiler NOT to do integer division Do this by specifying that it should treat the arguments as type float Do this by specifying that it should treat the arguments as type float This is called “type casting” This is called “type casting”


Download ppt "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 4: Enhancing Your Program."

Similar presentations


Ads by Google