Chapter03 - Expressions, Input, Output and Data Type Conversions
Chapter03 - Expressions, Input, Output and Data Type Conversions
These are
expressions
char ch = 'C';
cout << ch << " is stored as "
<< static_cast<int>(ch);
gallons = static_cast<int>(area/500);
avg = static_cast<double>(sum)/count;
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);
Reading in a character:
char ch;
cin >> ch; // Reads in any non-blank char
cin.get(ch); // Reads in any char
ch=cin.get();// Reads in any char
cin.ignore();// Skips over next char in
// the input buffer
string equals;
equals.assign(80,'=');
. . .
cout << equals << endl;
cout << "Total: " << total << endl;
• rand
– Returns a random number between 0 and the
largest int the computer holds
– Will yield the same sequence of numbers each
time the program is run
• srand(x)
– Initializes random number generator with
unsigned int x. x is the “seed value”.
– This should be called at most once in a program