1 / 36

ECE 1305 Introduction to Engineering and Computer Programming

ECE 1305 Introduction to Engineering and Computer Programming. Section 04 A Simple C++ Program. Program Listing. Program Listing (continued). Comments.

noel-deleon
Download Presentation

ECE 1305 Introduction to Engineering and Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 1305Introduction to Engineering and Computer Programming Section 04 A Simple C++ Program

  2. Program Listing

  3. Program Listing (continued)

  4. Comments The double slash ( //) instructs the compiler to ignore all text on the rest of the line. These lines may be used to include comments in the program listing as an aid to the reader. The IDE helps by printing the comments in green. Comments may occur anywhere in the code listing.

  5. Many of the functions needed in a program are not a part of C++, but are defined in external libraries that will be linked to the compiled program. #include is a preprocessor directive that instructs the compiler to include the external library. The external library is a header file whose name is enclosed in the brackets (< >). The compiler searches your computer hard drive for this file automatically. The IDE shows C++ keywords in blue. External Libraries

  6. In C++ a namespace is a collection of identifiers for functions, classes and objects. This statement gives us access to all of the functions in the external library iostream using the namespace called std. These functions are used for the input of data using the keyboard and the output of data to the monitor. Namespace

  7. Termination of Statements Statements in C++ are terminated with a semi-colon.

  8. Keyword const The keyword const defines a numerical value that will not be changed in the course of the program.

  9. Keyword double The keyword double instructs the compiler to allocate memory space for a double-precision floating point number.

  10. The programmer chose this name for the constant. Choose names that are easily understood Take care that names do not conflict with any words reserved for use by C++. The programmer followed a set of Style Rules that called for constants to be all upper case with underscores between words. The IDE shows user defined words in black. Constant Name

  11. Style Rules • Following consistent style rules helps the programmer. • The programmer must think about what is being accomplished and will write more organized code. • The code will be easier to read and debug. • Following consistent style rules helps others to read the code. • Following consistent style rules helps protect the company’s investment in the software.

  12. The Assignment Operator The assignment operator (=) assigns a value (in this case 2.0) to the memory location allocated for the constant named OXIDATION_FACTOR.

  13. The Main Function Every C++ Program has a main function. The function begins with this line and the statements that define this function are enclosed in braces { ... ... } int indicates that this function returns an integer as a result. The empty parentheses () indicates that no parameters are passed to the function.

  14. Variable Declarations The programmer defined an integer variable named sampleId. The compiler will allocate the required memory and associate that memory location with this variable name. The style rules call for variables to be readable, start with a lower case letter, and subsequent words start with an upper case letter.

  15. Variable Declarations The programmer defined a double-precision, floating point variable named sulfurContent. The compiler will allocate the required memory and associate that memory location with this variable name.

  16. Program Sections The programmer includes a comment to identify different sections of the program.

  17. Console Output The “console out” (cout) function is a part of the iostream library and prints data on the computer monitor. A window will automatically be opened.

  18. Insertion Operator The insertion operator (<<) takes the characters that follow and inserts them in the output stream. The text listed between the quotation marks (””) will appear on the screen. This text serves as a prompt for the user to respond to.

  19. Console Input The “console input” (cin) function is a part of the iostream library and accepts data from the keyboard.

  20. Insertion Operator The insertion operator (>>) function takes the data from the keyboard and inserts it into the memory location allocated for sampleId.

  21. Calculations A double precision variable is declared in the first part of the statement. This statement performs a simple calculation using the division operator (/) and the multiplication operator (*). The result of the calculation is stored in the variable named poundsDioxide using the assignment operator (=).

  22. Display Results Output endl to print a blank line on the screen.

  23. Concatenation Labels for the output data as well as the output data itself are displayed together on the screen. The text is concatenated to form a sentence. The endl is required to start a new line on the screen.

  24. The End of the Program The program ends by returning the integer 0. A brace (}) marks the end of the main function.

  25. Compiling the Program

  26. !!! Errors?

  27. Compile Again ... Success!

  28. Executing the Program

  29. Enter the Data

  30. More Data

  31. Even more Data

  32. Results

  33. Problems with this Program • There is no input data checking. • If the user puts in the wrong type of data, the output is meaningless. • The program would be better if it were “idiot-proof.”

More Related