190 likes | 361 Views
Integer Arithmetic. Operator Priority. Real Number Arithmetic. Mixed-Mode Arithmetic. When the operands are of the same type, the result is also of that type When the operands are of different types, the result is of the more inclusive type. double int char. More inclusive.
E N D
Mixed-Mode Arithmetic • When the operands are of the same type, the result is also of that type • When the operands are of different types, the result is of the more inclusive type double int char More inclusive
Assignment Statement • Form: <variable> = <expression>; • Example: x = y * z; 3 y Variables are names for memory locations 4 z 7 x
Mixed-Mode Assignment • When the variable is of a more inclusive type, the value copied to it is promoted to that type • When the variable is of a less inclusive type, the value copied to it is truncated and information may be lost
Mixed-Mode Assignment int i; double d; i = 2; d = i; // d contains 2.0
Mixed-Mode Assignment int i; double d; d = 3.56; i = d; // i contains 3
Type Cast Operations int i; double d; d = 3.56; i = d; // i contains 3 i = int(d); // same effect as above Form: <type name>(<expression>)
Interactive Input double payRate, hoursWorked, weeklyPay; cout << "Enter the pay rate: "; cin >> payRate; cout << "Enter the hours worked: "; cin >> hoursWorked; weeklyPay = hoursWorked * payRate; cout << "Weekly pay = " << weeklyPay << endl;
The ASCII Character Set 41 = ')'
Using apstring: Input and Output • #include "apstring.h" • apstring name; • cout << "Enter your last name: "; • cin >> name; • cout << "Your last name is " << name << endl; >> reads a word up to a space or newline character
Using apstring: getline • #include "apstring.h" • apstring name; • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your full name is " << name << endl; getline reads an entire line, including blanks, up to a newline character
Using >> Before getline • #include "apstring.h" • apstring name; • int age; • cout << "Enter your age: "; • cin >> age; • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your age is " << age << endl; • cout << "Your full name is " << name << endl; getline encounters the trailing newline after the age, stores an empty string in name, and does not wait for user input
Put >> After getline, or Do This • #include "apstring.h" • apstring name; • int age; • cout << "Enter your age: "; • cin >> age; • cin.ignore(); // consume \n • cout << "Enter your full name: "; • getline(cin, name); • cout << "Your age is " << age << endl; • cout << "Your full name is " << name << endl;