**This is an old revision of the document!**


#include<iostream>
#include<iomanip>
#include<string>
 
using namespace std;
 
int main() {
	cout << "\tPI:\n\n";
	// PI
	const double PI = 3.14159; // approaximately
	// I use this sometimes: const double PI = 4.0*atan(1.0);
	cout << "PI is " << PI << endl;
 
 
	// 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;
 
	// string type:
	// don't forget the include!
	string firstName = "Kevin";
	string lastName = "Seppi";
	cout << "My name is " << firstName << endl;
	string fullName = firstName + " " + lastName;
	cout << "my full name is " << fullName << endl;
	// string can be tell us more than just their contents:
	int nameLength = fullName.length();
	cout << "The length of my name is " << nameLength << endl;
	// extracting parts of a string, note that it starts with position 0
	cout << "The first character is \"" << fullName.substr(0,1) << "\"" << endl;
	// more interesting parts:
	string middlePart = fullName.substr(nameLength / 2 - 2, 6);
	cout << "The middle 6 characters are \"" << middlePart << "\"" << endl;
   cout << endl;
 
	cout << "CIN:" << endl;
 
	// from last time:
	int favoriteNumber;
	cout << "Please give me your favorite number:";
	cin >> favoriteNumber;
	cout << endl;
 
	cout << "Hey, I know you, your favorite number is " << favoriteNumber << endl;
 
	// we can do this with strings too
	string usersName;
	cout << "Please give me your first name: ";
	cin >> firstName;
	cout << endl;
 
	cout << firstName << " is a great name!!" << endl;
 
	//But be careful with stings:
	cout << "Please give me your full name: ";
	cin >> fullName;
	cout << endl;
 
	cout << "Full name: " << fullName << "?? really!!" << endl;
 
	cout << "Please let me try again, give me your full name: ";
	getline(cin,fullName);
	cout << endl;
 
	cout << "Full name: " << fullName << "That is better." << endl;
 
	// back to numbers
	// errors in cin
	int interestingNumber;
	cout << "I am expecting a number, try something unexpected:";
	cin >> interestingNumber;
	cout << endl;
	cout << "Interesting Number: " << interestingNumber << endl;
	cout << "What?" << endl;
	cout << endl;
	cout << "Maybe this will help, give bad input here:" << endl;
	cout << "Please give me bad input for your favorite number:";
	cin >> favoriteNumber;
	cout << endl;
 
	cout << "Hey, I know you, your favorite number is " << favoriteNumber << endl;
	cout << "Hmmm." << endl;
 
	// I sure wish I could tell when input failed and do something about it!!
 
	//system("pause");
	return 0;
}
cs-142/more-data-types-work.1473127590.txt.gz · Last modified: 2016/09/05 20:06 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0