C++ Programming Basics Chapter 2 Lecture CSIS 10A 1 Agenda Review An Intermediate Program Making a Program Interactive Assignment Statements Standard Program Structure File output Debugging 2 Review –what does this show? int x, y; x = 8; y = 6; cout << x << y; cout << x << " " << y; 3 Review –Identify each piece #include <iostream> using namespace std; int main() { // this program stores data in a variable int m; cout << “Enter the value of m:”; cin >> m; cout << “m = “ << m << endl; system(“pause”); return 0; } 4 Variables and Assignments Variables are like small blackboards We can write a number on them We can change the number We can erase the number C++ variables are names for memory locations We can write a value in them We can change the value stored there We cannot erase the memory location • Some value is always there 5 Review –what does this show? int x; x=6; x=8; cout << "x = " << x; 6 Identifiers Variable names are called identifiers Choosing variable names Use meaningful names that represent data to be stored First character must be • a letter • the underscore character _ Remaining characters must be • letters • Numbers • underscore character 7 Which identifiers are legal? feet 1993tax Sum Valid? FINAL final Average score Average_score Average.score 8 Keywords Keywords (also called reserved words) Are used by the C++ language Must be used as they are defined in the programming language Cannot be used as identifiers Examples (75 altogether): • main, while, if, new, for, do, class, struct, cin, cout 9 Declaring Variables Before use, variables must be declared Tells the compiler the type of data to store Examples: int one_weight, total_weight; int represents whole numbers • Could store 1, 4, -345, etc. • one_weight and total_weight are both of type int • Many other types, but for now, just use int!!! 10 Declaring Variables (Part 2) Put all your variable declarations at the top of main int main() { int score1, score2, sum; cin>> … etc … sum=score1+score2; … etc … return 0; } 11 Declaring Variables (Part 3) Declaration syntax: Type_name Variable_1 , Variable_2, . . . ; Declaration Examples: int m_score, total_score; float moon_distance; int age, num_students; char grade; ONLY USE TYPE int FOR NOW!!! 12 Agenda Review An Intermediate Program Making a Program Interactive Arithmetic Standard Program Structure Debugging 13 An intermediate C++ program: // height.cpp // Convert height in feet to inches. #include <iostream> using namespace std; int main() { int feet, inches; feet = 6; inches = feet * 12; cout << "Height is " << inches << " in."; return 0; } 14 Execution – what you see Height is 72 in. 15 Execution – what really happens int feet, inches; feet ? int inches ? int 16 Execution – what really happens feet=6; feet 6 int inches ? int 17 Execution – what really happens inches = feet * 12; feet 6.0 int 6*12 inches 72 int 18 Execution – what really happens cout << "Height is " << inches << " in."; feet 6.0 int inches Height is 72 in. 72 int 19 Agenda Review An Intermediate Program Making a Program Interactive Arithmetic Standard Program Structure Debugging 20 Interactive Program The previous program always runs exactly the same way (Height is 72 in) We’d like to be able to “put in” the data we want to process So we add two more lines. One asking for data: cout<<“What is height in feet?”<<endl; The other takes input: cin >> feet; 21 An intermediate C++ program: // height.cpp // Convert height in feet to inches. #include <iostream> using namespace std; int main() { int feet, inches; cout << “What is height in feet?” << endl; //******** cin >> feet; //******** inches = feet * 12; cout << "Height is " << inches << " in."; return 0; } 22 Basic Interactive Program int main() { 1) Declare Variables 2) Ask (prompt) for data 3) Input data 4) Calculate result 5) Display result with message return 0; } Go back a slide and number the lines Then run height.cpp and make interactive 23 Agenda Review An Intermediate Program Making a Program Interactive Arithmetic Standard Program Structure Debugging 24 Assignment Statements An assignment statement changes the value of a variable total_weight = one_weight + number_of_bars; • total_weight is set to the sum one_weight + number_of_bars Assignment statements end with a semi-colon The single variable to be changed is always on the left of the assignment operator ‘=‘ On the right of the assignment operator can be • Constants -- age = 21; • Variables -- my_cost = your_cost; • Expressions -- area = length * width; 25 Assignment Statements and Algebra The ‘=‘ operator in C++ is not an equal sign The following statement cannot be true in algebra number_of_bars = number_of_bars + 3; In C++ it means the new value of number_of_bars is the previous value of number_of_bars plus 3 26 Initializing Variables Declaring a variable does not give it a value Giving a variable its first value is initializing the variable Variables are initialized in assignment statements int miles, area; // declare the variables miles = 26; // initialize the variable area=0; // initialize Declaration and initialization can be combined int miles = 26, area = 0.0; Variables that are going to be input using cin>> or calculated later do not need to be initialized 27 Arithmetic in C++ Basic operations + - * A = pr2 F = (v -a) (v+a) / area=radius*radius*3.14; force=(v-a)/(v+a); More on arithmetic later. C++ has some interesting properties to be explored next chapter. 28 Can you guess what is stored in x? int a, b=3, c, x; a=b*2; c=a+3; x=c-2; a b c x ? 3 ? ? 6 3 ? ? 6 3 9 ? 6 3 9 7 Program flow is sequential (top to bottom) 29 Your turn…what is stored in x? a b c x int a, b, c, x=3; a=2; x=3+a; x=x+a; Program flow is sequential, order matters!! 30 Agenda Review An Intermediate Program Arithmetic Standard Program Structure Debugging 31 Standard Program Structure To solve most problems, your main() program will generally look like this (conceptually) 1. Declare variables for input, result and intermediate data 2. Ask for data 3. Input data (cin) 4. Calculate result 5. Output result (cout) 32 Another interactive C++ program: // cents2.cpp // Convert input number of nickels and dimes // to cents. #include <iostream> using namespace std; Notice cascading input int main() { int nickels, dimes, cents; cout << "Enter number of nickels and dimes: "; cin >> nickels >> dimes; cents = 5 * nickels + 10 * dimes; cout << nickels << " nickels and " << dimes << " dimes " << "= " << cents << " cents " << endl; return 0; } 33 Your Turn Run cents2.cpp and verify it works correctly Read p32 – 33 to see how to make the program write output to a file Modify the program to input dimes and quarters and calculate the equivalent cents 34 Agenda Review An Intermediate Program Arithmetic Standard Program Structure Debugging 35 Syntax Errors // errors.cpp #include <iostream> using namespace std; This program has 3 errors Run it and see what they are int main() { int n; cout << "Hello" << endl; N = 2; cout << "n eqalls " << n cout << "So long << endl; system("pause"); return 0; } 36 Tracking them down Syntax— double click on the first error message it takes you to that line of code Look at or above that line for possible errors Fix it and try again Errors often create other errors Just fix the first one, then recompile 37 That’s a wrap ! What we learned today: How to write an intermediate C++ program Standard program structure Assignments and Basic Arithmetic How to debug your program 38 Go back home proud ! You’re a C++ programmer ! 39