Full Transcript

CS 102 Computer Programming 1 Prepared by: Asst. Prof. Michelle C. Decamora, MIT Output ▸ The syntax of cout and miles; ▹ Causes computer to get a value of type double ▹ Places it in the variable miles 11 Input (Read) Statement (cont'd.) ▸ Using more tha...

CS 102 Computer Programming 1 Prepared by: Asst. Prof. Michelle C. Decamora, MIT Output ▸ The syntax of cout and miles; ▹ Causes computer to get a value of type double ▹ Places it in the variable miles 11 Input (Read) Statement (cont'd.) ▸ Using more than one variable in cin allows more than one value to be read at a time ▸ For example, if feet and inches are variables of type int, a statement such as: cin >> feet >> inches; ▹ Inputs two integers from the keyboard ▹ Places them in variables feet and inches respectively Input (Read) Statement (cont'd.) Preprocessor Directives ▸ C++ has a small number of operations such as arithmetic and assignment. ▸ Many functions and symbols needed to run a C++ program are provided as collection of libraries. ▸ Every library has a name and is referred to by a header file. Example: input/output (I/O) are contained in the header file iostream. power, absolute and sine are contained in the header file cmath. ▸ Preprocessor directives are processed by a program called a preprocessor. ▸ Preprocessor directives are commands supplied to the preprocessor ▸ All preprocessor commands begin with # ▸ No semicolon at the end of these commands. Preprocessor Directives (cont'd.) ▸ Syntax to include a header file: ▸ For example: #include ▹ Causes the preprocessor to include the header file iostream in the program. ▸ NOTE: Preprocessor directives to include header files are placed as the first line of a program so that the identifiers declared in those header files can be used throughout the program. namespace and Using cin and cout in a Program ▸ cin and cout are declared in the header file iostream, but within std namespace ▸ To use cin and cout in a program, use the following two statements: #include using namespace std; ▸ Appears after the statement #include. ▸ What will happen if you don’t put the using namespace std in your C++ source code? Using the string Data Type in a Program ▸ To use the string type, you need to access its definition from the header file string ▸ Include the following preprocessor directive: #include Creating a C++ Program ▸ C++ program has two parts: ▹ Preprocessor directives ▹ The program ▸ Preprocessor directives and program statements constitute C++ source code (.cpp) ▸ Compiler generates object code (.obj) ▸ Executable code is produced and saved in a file with the file extension.exe Creating a C++ Program (cont'd.) ▸ A C++ program is a collection of functions, one of which is the function main ▸ The first line of the function main is called the heading of the function: – int main() ▸ The statements enclosed between the curly braces ({ and }) form the body of the function – Contains two types of statements: ▹ Declaration statements ▹ Executable statements Creating a C++ Program (cont'd.) Creating a C++ Program (cont'd.) Sample Run: Line 9: firstNum = 18 Line 10: Enter an integer: 15 Line 13: secondNum = 15 Line 15: The new value of firstNum = 60 Debugging: Understanding and Fixing Syntax Errors ▸ Compile a program ▹ Compiler will identify the syntax error ▹ Specifies the line numbers where the errors occur Example2_Syntax_Errors.cpp c:\chapter 2 source code\example2_syntax_errors.cpp(9) : error C2146: syntax error : missing ';' before identifier 'num' c:\chapter 2 source code\example2_syntax_errors.cpp(11) : error C2065: 'tempNum' : undeclared identifier ▸ Learn how to spot and fix syntax errors Program Style and Form ▸ Every C++ program has a function main ▸ Programs must also follow syntax rules ▸ Other rules serve the purpose of giving precise meaning to the language Syntax ▸ Errors in syntax are found in compilation int x; //Line 1 int y //Line 2: error double z; //Line 3 y = w + x; //Line 4: error Use of Blanks ▸ In C++, you use one or more blanks to separate numbers when data is input ▹ Used to separate reserved words and identifiers from each other and from other symbols ▹ Must never appear within a reserved word or identifier Use of Semicolons, Brackets, and Commas ▸ All C++ statements end with a semicolon ▹ Also called a statement terminator ▸ { and } are not C++ statements ▸ Commas separate items in a list Semantics ▸ Possible to remove all syntax errors in a program and still not have it run ▸ Even if it runs, it may still not do what you meant it to do ▸ For example, 2 + 3 * 5 and (2 + 3) * 5 are both syntactically correct expressions, but have different meanings Prompt Lines ▸ Prompt lines : executable statements that inform the user what to do cout

Use Quizgecko on...
Browser
Browser