This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
cs-142:more-data-types-work [2016/09/05 20:06] kseppi |
cs-142:more-data-types-work [2016/09/10 15:35] (current) kseppi |
||
---|---|---|---|
Line 1: | Line 1: | ||
<code cpp> | <code cpp> | ||
#include<iostream> | #include<iostream> | ||
- | #include<iomanip> | ||
#include<string> | #include<string> | ||
Line 7: | Line 6: | ||
int main() { | int main() { | ||
- | cout << "\tPI:\n\n"; | + | |
- | // PI | + | /* Try this for input: |
- | const double PI = 3.14159; // approaximately | + | 42 |
- | // I use this sometimes: const double PI = 4.0*atan(1.0); | + | Kevin |
- | cout << "PI is " << PI << endl; | + | Kevin D Seppi |
+ | Kevin | ||
+ | Kevin | ||
+ | */ | ||
- | |||
- | // Formatting role play, then | ||
- | cout << endl << "\tFORMATTING:\n\n"; | ||
- | double aCoolNumber = PI; | ||
- | int anotherCoolNumber = 7; | ||
- | cout << "The way it comes out by default: " << aCoolNumber << endl; | ||
- | // we can change that: (note #include of iomanip!) | ||
- | cout << "The way it comes out modified by fixed and setprecision: " << fixed << setprecision(3) << aCoolNumber << endl; | ||
- | cout << "The way it comes out modified by fixed and setprecision (rounding!): " << fixed << setprecision(4) << aCoolNumber << endl; | ||
- | cout << "We round the output but not the value itself: " << fixed << setprecision(5) << aCoolNumber << endl; | ||
- | cout << "The way it comes out modified by setw too small (*10000 to make it big): " << setw(3) << aCoolNumber*10000 << endl; | ||
- | cout << "The way it comes out modified by setw more than big enough: " << setw(13) << aCoolNumber << endl; | ||
- | cout << "The way it comes out modified by setw but only affect next item: " << setw(13) << anotherCoolNumber << " and " << aCoolNumber << endl; | ||
- | cout << "The way it comes out modified by setw on both items: " << setw(13) << anotherCoolNumber << " and " << setw(13) << aCoolNumber << endl; | ||
- | cout << "The way it comes out enlarging the precision: " << fixed << setprecision(8) << aCoolNumber << endl; | ||
- | |||
- | cout << endl << "\tROUNDING:\n\n"; | ||
- | |||
- | // from last time: c++ does not round | ||
- | double priceOfCar = 12345.53; | ||
- | int roundedPrice = priceOfCar; | ||
- | cout << "price " << priceOfCar << " or rounded is " << roundedPrice << " dollars??\n"; | ||
- | // but you can make it round: | ||
- | roundedPrice = priceOfCar + 0.5; | ||
- | cout << "price " << priceOfCar << " or rounded is " << roundedPrice << " dollars.\n"; | ||
- | // rounded to the nearest 100 | ||
- | cout << "price " << priceOfCar << " or rounded to the nearest 100 is " << 100*(static_cast<int>(priceOfCar/100+0.5)) << " dollars.\n"; // is / ok? | ||
- | cout << endl; | ||
- | |||
cout << "strings:" << endl; | cout << "strings:" << endl; | ||
Line 51: | Line 24: | ||
string fullName = firstName + " " + lastName; | string fullName = firstName + " " + lastName; | ||
cout << "my full name is " << fullName << endl; | cout << "my full name is " << fullName << endl; | ||
+ | // Remember that this works: | ||
+ | cout << "Some sum! " << 23 + 34 << endl; | ||
+ | // But this will not: | ||
+ | // cout << "Failed Concatination: " << "some"+"more" << endl; | ||
+ | |||
// string can be tell us more than just their contents: | // string can be tell us more than just their contents: | ||
int nameLength = fullName.length(); | int nameLength = fullName.length(); | ||
Line 59: | Line 37: | ||
string middlePart = fullName.substr(nameLength / 2 - 2, 6); | string middlePart = fullName.substr(nameLength / 2 - 2, 6); | ||
cout << "The middle 6 characters are \"" << middlePart << "\"" << endl; | cout << "The middle 6 characters are \"" << middlePart << "\"" << endl; | ||
- | cout << endl; | + | cout << endl; |
- | | + | |
cout << "CIN:" << endl; | cout << "CIN:" << endl; | ||
Line 90: | Line 68: | ||
cout << endl; | cout << endl; | ||
- | cout << "Full name: " << fullName << "That is better." << endl; | + | cout << "Full name: " << fullName << " What?" << endl; |
// back to numbers | // back to numbers |