Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 1 Sept 2006. Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.

Similar presentations


Presentation on theme: "CS 1400 1 Sept 2006. Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return."— Presentation transcript:

1 CS 1400 1 Sept 2006

2 Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return 0; }

3 Output… Pseudocode Print “Enter a number: “ C++ cout << “Enter a number: “; Pseudocode Print square C++ cout << square;

4 Input… Pseudocode Get value from user C++ cin >> value;

5 Assignments… Pseudocode Square = n * n C++ square = n * n; Pseudocode Pay = hours * 40 + (hours-40) * Overtime_pay C++ pay = hours * 40 + (hours-40) * overtime_pay;

6 Declarations… All variables must be established or declared prior to use – usually at the top of the program Integers are declared using int. For example; int value; int width, height, depth; A variable name must begin with a letter, followed by letters, digits, or underscores.

7 Pseudocode Example: “Write a program to calculate the volume of a box, using dimensions provided by the user.” Print “Enter height, width, and depth of box:” Get height from user Get width from user Get depth from user volume = height * width * depth Print “Volume is: “ Print volume

8 C++ boilerplate… // Cannon, demo program #include using namespace std; int main() { return 0; }

9 Adding C++ instructions… // Cannon, demo program #include using namespace std; int main() {int height, width, depth, volume; cout << “Enter height, width, and depth: “; cin >> height >> width >> depth; volume = height * width * depth; cout << “Volume is: “ << volume; return 0; }

10 C++ is case sensitive! Use lower case for simplicity Upper case or underscore may be used to separate multi-word variable names int phone_number;// easiest to read int phoneNumber;// easier to read int phonenumber;// not as easy to read

11 Extended syntax… Input and output statements can be cascaded; cin >> height >> width >> depth; cout << “the value of width is: “ << width; Declarations can be a list; int height, width, depth, volume; Output messages can include control characters such as \n (new line) and \a (beep) cout << “This appears on line 1 \n This is on line 2”; Output messages may output endl to force a new line cout << “this is line1 “ << endl << “This is line 2”;

12 Chapter 2 issues -- Key words; Table 2.4 (4 th edition) Gloss over (for now) –Section 2.3 –Section 2.7 –Section 2.9 –Section 2.10 –Section 2.16

13 Integer variables Declaring variables as type int – –Only allows them to hold an integer (+ or -) –Range -2,147,483,648 to 2,147,483,647 Variations – –unsigned int –long –short… etcetera

14 Floating point variables… Declaring variables to be type float – –Allows them to hold an approximate floating- point value (precision to ~6 digits) –Range +/- 3.4 x 10 -38 to +/- 3.4 x 10 38 –Constants may be expressed in floating-point notation or E-notation –E-notation is like scientific notation 3.14 x 10 5 is the same as 3.14E5


Download ppt "CS 1400 1 Sept 2006. Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return."

Similar presentations


Ads by Google