**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 = 4.0*atan(1.0);
	cout << "PI is " << PI << endl;
 
 
	// Formatting role play, then
	cout << endl << "\tFORMATTING:\n\n";
	double a_cool_number = PI;
	int another_cool_number = 7;
	cout << "The way it comes out by default: " << a_cool_number << endl;
	// we can change that: (note #include of iomanip!)
	cout << "The way it comes out modified by fixed and setprecision: " << fixed << setprecision(3) << a_cool_number << endl;
	// we can try to change that:
	cout << "The way it comes out modified by setw too small: " << setw(3) << a_cool_number << endl;
	// we can change that:
	cout << "The way it comes out modified by setw more than big enough: " << setw(13) << a_cool_number << endl;
	// we can change that:
	cout << "The way it comes out modified by setw but only affect next thing " << setw(13) << another_cool_number << " and " << a_cool_number << endl;
	// We have not changed a_cool_number
	cout << "The way it comes out enlarging the precision: " << fixed << setprecision(8) << a_cool_number << endl;
 
	cout << endl << "\tROUNDING:\n\n";
 
	// from last time: c++ does not round
	double price = 12345.53;
	int about_dollars = price;
	cout << "price " << price << " or rounded is " << about_dollars << " dollars??\n";
	// but you can ask it to:
	about_dollars = round(price);
	cout << "price " << price << " or rounded is " << about_dollars << " dollars.\n";
	// round was added in c++11 so it may not work in earlier versions. Also Microsoft
	// did not put this feature in visual studio until the 2013 version.
	// The old way to round was:
	about_dollars = price + 0.5;
	cout << "price " << price << " or rounded is " << about_dollars << " dollars.\n";
	// rounded to the nearest 100
	cout << "price " << price << " or rounded to the nearest 100 is " << 100*round(price/100) << " dollars.\n"; // is / ok?
 
	cout << endl << "\tstring:\n\n";
 
	// string type:
	// don't forget the include!
	string first_name = "Kevin";
	string last_name = "Seppi";
	cout << "My name is " << first_name << endl;
	string full_name = first_name + " " + last_name;
	cout << "my full name is " << full_name << endl;
	// string functions are different from the ones we have seen so far atan and round
	int name_length = full_name.length();
	cout << "The length of my name is " << name_length << endl;
	// other string functions: _verb, nor for you.
	// extracting parts of a string, note that it starts with position 0
	cout << "The first character is \"" << full_name.substr(0,1) << "\"" << endl;
	// more interesting parts:
	string middle_part = full_name.substr(name_length / 2 - 2, 6);
	cout << "The middle 6 characters are \"" << middle_part << "\"" << endl;
 
	cout << endl << "\tCIN:\n\n";
 
	// from last time:
	int favorite_number;
	cout << "Please give me your favorite number:";
	cin >> favorite_number;
 
	cout << "\nHey, I know you, your favorite number is " << favorite_number << endl;
 
	// try again but give "abc" as your favorite number.
	// I wish we had a way to check for bad input and do some thing about it!
 
	// we can do this with strings too
	int users_name;
	cout << "Please give me your first name: ";
	cin >> first_name;
 
	cout << first_name << " is a funny name!!\n";
 
	//But be careful with stings:
	cout << "Please give me your full name: ";
	cin >> full_name;
 
	cout << full_name << " really!!\n";
	cin >> last_name;
	cout << last_name << " is the rest of it.\n";
 
	// back to numbers
	// errors in cin
	int interesting_number;
	cout << "I am expecting a number, try something unexpected:";
	cin >> interesting_number;
	cout << "\nI saw: " << interesting_number << endl;
	cout << "What?\n\nMaybe this will help, give bad input here:\n";
	cout << "Please give me your favorite number:";
	cin >> favorite_number;
 
	cout << "\nHey, I know you, your favorite number is " << favorite_number << 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.1410880941.txt.gz · Last modified: 2014/09/16 09:22 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