ECE151 - Lecture 3
ECE151 - Lecture 3
Programming
The cin Object
These are
expressions
Order of Operations
• In the expression 2 + 2 * 2 – 2 ,
Evaluate Evaluate Evaluate
2nd 1st 3rd
Associativity of Operators
long double
• Highest double
float
unsigned long
long
unsigned int
int
unsigned short
short
• Lowest char
• Ranked by largest number they can hold
Type Coercion
char ch = 'C';
cout << ch << " is stored as "
<< static_cast<int>(ch);
gallons = static_cast<int>(area/500);
avg = static_cast<double>(sum)/count;
Older Type Cast Styles
no ;
#define goes here
x += 5; means x = x + 5;
x -= 5; means x = x – 5;
x *= 5; means x = x * 5;
x /= 5; means x = x / 5;
x %= 5; means x = x % 5;
The right-hand side is evaluated before the
combined assignment operation is done.
x *= a + b; means x = x * (a + b);
Formatting Output
Reading in a character
char ch;
cin >> ch; // Reads in any non-blank char
cin.get(ch); // Reads in any char
cin.ignore(); // Skips over next char in
// the input buffer
String Operators – Cont.
• Reading in a C-string
const int SIZE = 10;
char Cstr[SIZE];
cin >> Cstr; // Reads in a C-string with no
// blanks. Will write past the
// end of the array if input string
// is too long.
cin.getline(Cstr, 10);
// Reads in a C-string that may
// contain blanks. Ensures that <= 9
// chars are read in.
• Can also use setw() and width() to control input field
widths
C-String Initialization vs. Assignment