Wednesday, April 10, 2013 2:00 PM

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lecture 4

Wednesday, April 10, 2013 2:00 PM

double: can hold fraction 10 to the -308 up to 10 to the 308 about 15 significant digits ex. -0.10, .123, 12.34, 56. int: holding only integers, generally limited to about -2 billion to +2 billion ex. 123, -45 drops fraction if entered double ex. int k - 123.8898; // k will be 123, drops the fraction. BE CAREFUL compiler warning, maybe legal but not what author intended DON'T IGNORE WARNINGS compiler can convert int to double opeartions +-*/ both operands double, double both operands int, int 14.3 / 5.0 = 2.86 14.3 / 5 = 2.86 14/ 5.0 = 2.8 14/5 = 2 // fraction dropped % is the remainder / modulus operator 14 %[mod] 5 ==> 4 useful for page number [pg #] % 2 ==> 0 for even, 1 for odd calendar applications (feb. 29) double x = 3.1 + 14/5; // x is 5.1 3.1 + 2 <== opearted in int., thus x = 5.1 double y = 2/3 * x; 2.0/3 * x; // y is 0.0

int a = 10; int b = a*a; int c = 25/(b-100); undefined behavior double d; double e = 2 * d; cout << e;

//Error, d is uninitialized, but we're trying to use its value

likely to be runtime error int f = 1000; int g = f * f * f; int h = f * g; // technically undefined, but practically, h gets a weird value h = 1 trillion , too big to be an int take lower of 32 bits, like odometer 99999 ==> 00000 after 1 more mile

What is your name? Sir Robin How old are you? 32 What is your quest? To seek the Holy Grail Hello, brave Sir Robin! You want To seek the Holy Grail If you live, next year you will be 33 ========================================= #include <iostream> using namespace std; int main () { cout << "What is your name? "; string personsName; getline(cin, personsName); // getline function in the library, cin input source // (input, string) arguments for getline function cin >> personsName; //Not what we want cout << "How old are you? "; int age; cin >> age; // never consumes newline character from 32 [enter] //...do something to discard the rest of the line cin.ignore(10000, '\n'); // MAKE SURE IT'S A BACKSLASH // "/n" is wrong // consume and throw away 10000 characters until you reach a newline character // don't go over 1billion, or weird things happen cout << "What is your quest? "; string quest; getline(cin, quest); cout << "Hello, brave " << personsName <<" ,, endl; cout << "You want " << quest << endl; cout << "If you live, next year you will be " << (age+1) << endl; }

What is yoru name? Sir Robin

How old are you? 32 What is your quest? Hello, brave Sir Robin! You want If you live, next year you will be 33 // problem because read in cin then getline

personsName: (empty string) persons Name: =====> Sir Robin quest: To seek the Holy Grail

What is your name? Sir What is your quest? Hello, brave Sir! You want Robin // read the first word, Sir, left "Robin" for later input to consume. Program does not wait for you to input "What is your quest"

personsName: Sir quest: [space]Robin

How many hours did you work? 40 You typed 40 What is your hourly rate of pay? 12.13 You earned $485.2 ============================================= #include <iostream> #include <iomanip> // for "fixed" and "setprecision" using namespace std;

int main() { // Gather input data std::cout<< "How many hours did you work? "; //prompt double hoursWorked; //variable cin >> hoursWorked; //input variable cout << "You typed " << hoursWorked; // output testcode //scaffolding to help see what's going on, can take out afterwards //prompt for hours worked cout << "What is your hourly rate of pay?"; double payRate; cin >> payRate; //prompting for pay // Compute earnings and withholding cout << fixed << setprecision(2); double amtEarned = hoursWorked * payRate; cout << "You earned $" << amtEarned << endl; if (payRate >= 12.00) { cout << "$" << (0.10 * amtEarned) << "will be withheld. "; } else { cout << "$" << (0.05 * amtEarned) << "will be withheld. "; } // pay 10% taxes if you earn at least $12/hour // 5% everything less than $12.00 }

if (condition) statementIfTrue else statmentIfFalse someThing someThing someThing someThing someThing someThing > anotherThing >= anotherThing < anotherThing <= anotherThing != anotherThing (not equal to) == anotherThing (is equal to)

BOUNDARY CASE ERRORS Did you mean greater than and equal to? at least ==> include "=" //need string of characters, type variable; double hoursWorked; string personName can't do arithmetic with strings, have to use cin

cin does not read [enter] or newline character remedy: cin.ignore(10000, '\n');

You might also like