Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3, Part 2 s Input: cin s Example programs.

Similar presentations


Presentation on theme: "Chapter 3, Part 2 s Input: cin s Example programs."— Presentation transcript:

1

2 Chapter 3, Part 2 s Input: cin s Example programs

3 Cin Object – Reading in Data cin >> my_num; The keyboard entry is stored into a local variable called my_num. Read as: “get from my_num”.

4 Examples of cin cin >> var1; Syntax: cin >> var1; Examples: cin >> last_name;// string cin >> ch;// character cin >> my_id#;// number * Can only determine data types by declaration statement

5 Multiple cin on One Line cin >> var1 >> var2 >>... Syntax: cin >> var1 >> var2 >>... cin >> First >> Last >> MyNum; * cin >> qz1 >> qz2 >> qz3 >> qz4; cin >> First; cin >> Last; cin >> MyNum; cin >> qz1 >> qz2 >> qz3 >> qz4;

6 cin & cout standard device standard device (keyboard) (screen) main() { cout << cin >> }

7 cin & cout cout cin > insertion extraction “put to” “get from” For input: whitespace characters used as a separator, otherwise ignored

8 cin Example 1 int num1, num2, num3; double average; cout << "Enter three integer numbers: "; cin >> num1 >> num2 >> num3; average = (num1 + num2 + num3) / 3.0; cout << "The average is " << average; cout << '\n';

9 cin Example 1 Output: 3 5 5 The average is 4.33333

10 cin Example 2 double radius, circumference; double pi = 3.1416; cout << "Enter the radius of a circle: "; cin >> radius; circumference = 2 * pi * radius; cout << "The circumference of a circle of radius " << radius << " is " << circumference <<‘\n’;

11 cin Example 2 Output: Enter the radius of a circle: 14 The circum... circle of radius 14 is 87.9648

12 cin Example 3 double celsius, faren; cout <<"Enter the temperature in degrees Fahrenheit: "; cin >> faren; celsius = 5.0/9.0 * (faren - 32.0); cout << faren <<" degrees Fahrenheit equals "<< celsius << " degrees celsius.\n";

13 cin Example 3 Output: Enter the temperature in degrees Farenheit: 82 82 degrees Farenheit equals 27.7777 degrees celsius.

14 const Qualifier Revisited const DataType Name = Literal Value; Syntax: const DataType Name = Literal Value; Examples const double PI = 3.1416; const double OT_RATE = 1.5; * Protected from change within the program Don’t believe me… try it! PI = 3.14159;cin >> OT_RATE;

15 const Qualifier double radius, area; const double PI = 3.1416 cout > radius; cout << “The area of your circle is “ << PI * radius * radius; * How can that last statement be rewritten using pow?

16 Common Programming Errors 3 not initializing variables before use >> 3 forgetting >> to separate variables in cin ++ -- 3 applying ++ or -- to an expression (x-y)++

17 Why isn’t phonetic spelled as it sounds? Why do they put Braille dots on the keypad of a drive-up ATM? How many Microsoft technicians does it take to change a light bulb? How many Microsoft technicians does it take to change a light bulb? Three: two holding the ladder and one to screw the bulb into a faucet. “Lack of brains hinders research” The Columbus Dispatch, 16 April 1996


Download ppt "Chapter 3, Part 2 s Input: cin s Example programs."

Similar presentations


Ads by Google