1.29k likes | 1.42k Views
Chapter 3. Expressions and Interactivity. 3.1 The cin Object. The cin object reads information types at the keyboard. cin is the standard input object Notice the >> and << operators appear to point in the direction information is flowing. Program 3-1. #include <iostream.h>
E N D
3.1 The cin Object • The cin object reads information types at the keyboard. • cin is the standard input object • Notice the >> and << operators appear to point in the direction information is flowing.
Program 3-1 #include <iostream.h> void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"What is the length of the rectangle? "; cin>>length; cout <<"What is the width of the rectangle? "; cin>>width; area = length * width; cout <<"The area of the rectangle is " << area << ".\n"; }
Program Output This program calculates the area of a rectangle. What is the length of the rectangle? 10 [Enter] What is the width of the rectangle? 20 [Enter] The area of the rectangle is 200.
Program 3-2 // This program reads the length and width of a rectangle. // It calculates the rectangle's area and displays // the value on the screen. #include <iostream.h> void main(void) { int length, width, area; cin >> length; cin >> width; area = length * width; cout << "The area of the rectangle is " << area << endl; }
Entering Multiple Values • The cin object may be used to gather multiple values at once.
Program 3-3 #include <iostream.h> void main(void) { int length, width, area; cout <<"This program calculates the area of a rectangle.\n"; cout <<"Enter the length and width of the rectangle separated by a space. \n"; cin >> length>> width; area = length * width; cout <<"The area of the rectangle is " << area << endl; }
Program Output This program calculates the area of a rectangle. Enter the length and width of the rectangle separated by a space. 10 20 [Enter] The area of the rectangle is 200
Program 3-4 // This program demonstrates how cin can read multiple values // of different data types. #include <iostream.h> void main(void) { int whole; float fractional; char letter; cout << "Enter an integer, a float, and a character: "; cin >> whole >> fractional >> letter; cout << "whole: " << whole << endl; cout << "fractional: " << fractional << endl; cout << "letter: " << letter << endl; }
Program Output Enter an integer, a float, and a character: 4 5.7 b [Enter] whole: 4 fractional: 5.7 letter: b
Reading Strings • cin can read strings as well as numbers. • Strings are stored in character arrays. char Company[12];
Program 3-5 #include <iostream.h> #include<string> void main(void) { string name; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }
Program Output What is your name? Charlie [Enter] Good morning Charlie
Program 3-6 // This program demonstrates how cin can read a // string into a character array #include <iostream.h> void main(void) { char name[21] ; cout << "What is your name? "; cin >> name; cout << "Good morning " << name << endl; }
Program Output What is your name? Charlie [Enter] Good morning Charlie
Program 3-7 // This program reads two strings into two character arrays. #include <iostream.h> void main(void) { char first[16], last[16]; cout << "Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }
Program Output Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny
Notes on strings: • If a character array is intended to hold strings, it must be at least one character larger than the largest string that will be stored in it. • The cin object will let the user enter a string larger than the array can hold. If this happens, the string will overflow the array’s boundaries and destroy other information in memory. • If you wish the user to enter a string that has spaces in it, you cannot use this input method.
3.2 Focus on Software Engineering: Mathematical Expressions • C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.
Program 3-7 // This program reads two strings into two character arrays #include <iostream.h> void main(void) { char first[16], last[16]; cout << Enter your first and last names and I will\n"; cout << "reverse them.\n"; cin >> first >> last; cout << last << ", " << first << endl; }
Program 3-7 Output with Example Input Enter your first and last names and I will reverse them. Johnny Jones [Enter] Jones, Johnny
Program 3-8 // This program asks the user to enter the numerator // and denominator of a fraction and it displays the // decimal value #include <iostream.h> void main(void) { float numerator, denominator; cout << "This program shows the decimal value of "; cout << "a fraction.\n"; cout << “Enter the numerator: “; cin >> numerator; cout << “Enter the denominator: “; cin >> denominator; cout << “The decimal value is “; cout << (numerator / denominator); }
Program Output for Program 3-8 with Example Input This program shows the decimal value of a fraction. Enter the numerator: 3 [Enter] Enter the denominator: 6 [Enter] The decimal value is 0.1875
Table 3-1 Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) -* / %+ -
Associativity • If two operators sharing an operand have the same precedence, they work according to their associativity, either right to left or left to right
No Exponents Please! • C++ does not have an exponent operator. • Use the pow() library function to raise a number to a power. • Will need #include <math.h> for pow() function. area = pow(4,2) // will store 42 in area
Program 3-9 // This program calculates the area of a circle. // The formula for the area of a circle is Pi times // the radius squared. Pi is 3.14159. #include <iostream.h> #include <math.h> // needed for the pow function void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = 3.14159 * pow(radius,2); cout << "The area is " << area; }
Program 3-9 Output With Example Input This program calculates the area of a circle. What is the radius of the circle? 10[Enter] The area is 314.159
3.3 When you Mix Apples and Oranges: Type Coercion • When an operator’s operands are of different data types, C++ will automatically convert them to the same data type.
Type Coercion Rules: • Rule 1: Chars, shorts, and unsigned shorts are automatically promoted to int. • Rule 2: When an operator works with two values of different data types, the lower-ranking value is promoted to the type of the higher-ranking value. • Rule 3: When the final value of an expression is assigned to a variable, it will be converted to the data type of the variable.
3.4 Overflow and Underflow • When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows. • Overflow - when a variable is assigned a number that is too large for its data type • Underflow - when a variable is assigned a number that is too small for its data type
Program 3-10 // This program demonstrates integer underflow and // overflow #include <iostream.h> void main(void) { short testVar = 32767; cout << testVar << endl; testVar = testVar + 1; cout << testVar << endl; testVar = testVar - 1; cout << testVar << endl; }
Program Output 32767 -32768 32767
Program 3-11 // This program can be used to see how your system // handles floating point overflow and underflow. #include <iostream.h> void main(void) { float test; test = 2.0e38 * 1000; // Should overflow test cout << test << endl; test = 2.0e-38 / 2.0e38; cout << test << endl; }
3.5 The Typecast Operator • The typecast operator allows you to perform manual data type conversion. Val = int(number); //If number is a floating //point variable, it will be //truncated to an integer and //stored in the variable Val
Program 3-12 #include <iostream.h> void main(void) { int months, books; float perMonth; cout << "How many books do you plan to read? "; cin >> books; cout << "How many months will it take you to read them? "; cin >> months; perMonth = float(books) / months; cout << "That is " << perMonth << " books per month.\n"; }
Program Output How many books do you plan to read? 30 [Enter] How many months will it take you to read them? 7 [Enter] That is 4.285714 books per month.
Typecast Warnings • In Program 3-11, the following statement would still have resulted in integer division: perMonth = float(books / months); • Because the division is performed first and the result is cast to a float.
Program 3-13 // This program uses a typecast operator to print // a character from a number. #include <iostream.h> void main(void) { int number = 65; cout << number << endl; cout << char(number) << endl; }
Program Output 65 A
3.6 The Power of Constants • Constants may be given names that symbolically represent them in a program.
Program 3-14 // This program calculates the area of a circle. #include <iostream.h> #include <math.h> void main(void) { const float pi = 3.14159; double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = pi * pow(radius,2); cout << "The area is " << area; }
The #define Directive • The older C-style method of creating named constants is with the #define directive, although it is preferable to use the const modifier. #define PI 3.14159 • is roughly the same as const float PI=3.14159;
Program 3-15 #include <iostream.h> #include <math.h> // needed for pow function #define PI 3.14159 void main(void) { double area, radius; cout << "This program calculates the area of a circle.\n"; cout << "What is the radius of the circle? "; cin >> radius; area = PI * pow(radius, 2); cout << "The area is " << area; }
3.7 Multiple Assignment and Combined Assignment • Multiple assignment means to assign the same value to several variables with one statement. A = B = C = D = 12; Store1 = Store2 = Store3 = BegInv;
Program 3-16 // The program tracks the inventory of three widget stores // that opened at the same time. Each store started with the // same number of widgets in inventory. By subtracting the // number of widgets each store has sold from its inventory, // the current inventory can be calculated. #include <iostream.h> void main(void) { int begInv, sold, store1, store2, store3; cout << “One week ago, 3 new widget stores opened\n"; cout << “at the same time with the same beginning\n"; cout << “inventory. What was the beginning inventory? "; cin >> begInv; store1 = store2 = store3 = begInv;
Program 3-16 Continued cout << "How many widgets has store 1 sold? "; cin >> sold; store1 = store1 – sold; //Subtract sold from store1 cout << "How many widgets has store 2 sold? "; cin >> sold; store2 = store2 – sold; //Subtract sold from store2 cout << "How many widgets has store 3 sold? "; cin >> sold; store3 = store3 – sold; //Subtract sold from store 3 cout << "Store1: " << store1 << endl; cout << "Store2: " << store2 << endl; cout << "Store3: " << store3 << endl; }