Chapter 3, Part 2 s Input: cin s Example programs
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”.
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
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;
cin & cout standard device standard device (keyboard) (screen) main() { cout << cin >> }
cin & cout cout cin > insertion extraction “put to” “get from” For input: whitespace characters used as a separator, otherwise ignored
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';
cin Example 1 Output: The average is
cin Example 2 double radius, circumference; double pi = ; 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’;
cin Example 2 Output: Enter the radius of a circle: 14 The circum... circle of radius 14 is
cin Example 3 double celsius, faren; cout <<"Enter the temperature in degrees Fahrenheit: "; cin >> faren; celsius = 5.0/9.0 * (faren ); cout << faren <<" degrees Fahrenheit equals "<< celsius << " degrees celsius.\n";
cin Example 3 Output: Enter the temperature in degrees Farenheit: degrees Farenheit equals degrees celsius.
const Qualifier Revisited const DataType Name = Literal Value; Syntax: const DataType Name = Literal Value; Examples const double PI = ; const double OT_RATE = 1.5; * Protected from change within the program Don’t believe me… try it! PI = ;cin >> OT_RATE;
const Qualifier double radius, area; const double PI = cout > radius; cout << “The area of your circle is “ << PI * radius * radius; * How can that last statement be rewritten using pow?
Common Programming Errors 3 not initializing variables before use >> 3 forgetting >> to separate variables in cin applying ++ or -- to an expression (x-y)++
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