#include<iostream>
#include<string>
 
using namespace std;
 
int main() {
 
   /* Try this for input:
      42
      Kevin
      Kevin D Seppi
      Kevin
      Kevin
   */
 
	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;
	// 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:
	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 << " What?" << 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.txt · Last modified: 2016/09/10 21:35 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