370 likes | 494 Views
ECE 1305 Introduction to Engineering and Computer Programming. Section 04 A Simple C++ Program. Program Listing. Program Listing (continued). Comments.
E N D
ECE 1305Introduction to Engineering and Computer Programming Section 04 A Simple C++ Program
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.
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
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
Termination of Statements Statements in C++ are terminated with a semi-colon.
Keyword const The keyword const defines a numerical value that will not be changed in the course of the program.
Keyword double The keyword double instructs the compiler to allocate memory space for a double-precision floating point number.
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
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.
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.
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.
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.
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.
Program Sections The programmer includes a comment to identify different sections of the program.
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.
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.
Console Input The “console input” (cin) function is a part of the iostream library and accepts data from the keyboard.
Insertion Operator The insertion operator (>>) function takes the data from the keyboard and inserts it into the memory location allocated for sampleId.
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 (=).
Display Results Output endl to print a blank line on the screen.
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.
The End of the Program The program ends by returning the integer 0. A brace (}) marks the end of the main function.
!!! Errors?
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.”