Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs-142:more-data-types-work [2014/09/16 09:22]
kseppi created
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>​
 + 
 using namespace std; using namespace std;
 + 
 int main() { int main() {
- cout << "​\tPI:​\n\n";​ +    
- // PI +   ​/* Try this for input
- const double PI = 4.0*atan(1.0);​ +      42 
- cout << "PI is " << PI << endl; +      Kevin 
- +      Kevin D Seppi 
- +      Kevin 
- // Formatting role play, then +      Kevin 
- cout << endl << "​\tFORMATTING:\n\n"; +   *
- double a_cool_number = PI; +  
- int another_cool_number = 7; + cout << "strings:" << endl; 
- 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:  // string type:
  // don't forget the include!  // don't forget the include!
- string ​first_name ​= "​Kevin";​ + string ​firstName ​= "​Kevin";​ 
- string ​last_name ​= "​Seppi";​ + string ​lastName ​= "​Seppi";​ 
- cout << "My name is " << ​first_name ​<< endl; + cout << "My name is " << ​firstName ​<< endl; 
- string ​full_name ​first_name ​+ " " + last_name+ string ​fullName ​firstName ​+ " " + lastName
- cout << "my full name is " << ​full_name ​<< endl; + cout << "my full name is " << ​fullName ​<< endl; 
- // string ​functions are different from the ones we have seen so far atan and round + // Remember that this works: 
- int name_length ​full_name.length();​ + cout << "Some sum! " << 23 + 34 << endl; 
- cout << "The length of my name is " << ​name_length ​<< endl; + // But this will not: 
- // other string functions: _verb, nor for you.+ // 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  // extracting parts of a string, note that it starts with position 0
- cout << "The first character is \""​ << ​full_name.substr(0,​1) << "​\""​ << endl;+ cout << "The first character is \""​ << ​fullName.substr(0,​1) << "​\""​ << endl;
  // more interesting parts:  // more interesting parts:
- string ​middle_part ​full_name.substr(name_length ​/ 2 - 2, 6); + string ​middlePart ​fullName.substr(nameLength ​/ 2 - 2, 6); 
- cout << "The middle 6 characters are \""​ << ​middle_part ​<< "​\""​ << endl; + cout << "The middle 6 characters are \""​ << ​middlePart ​<< "​\""​ << endl; 
- +        cout << endl
- cout << endl << "\tCIN:\n\n"; +  
 + cout << "CIN:" ​<< endl
 + 
  // from last time:  // from last time:
- int favorite_number;+ int favoriteNumber;
  cout << "​Please give me your favorite number:";​  cout << "​Please give me your favorite number:";​
- cin >> ​favorite_number+ cin >> ​favoriteNumber;​ 
- + cout << endl
- cout << "\nHey, I know you, your favorite number is " << ​favorite_number ​<< endl; +  
- + cout << "Hey, I know you, your favorite number is " << ​favoriteNumber ​<< 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  // we can do this with strings too
- int users_name;+ string usersName;
  cout << "​Please give me your first name: ";  cout << "​Please give me your first name: ";
- cin >> ​first_name+ cin >> ​firstName;​ 
- + cout << endl
- cout << ​first_name ​<< " is a funny name!!\n"; +  
 + cout << ​firstName ​<< " is a great name!!" ​<< endl
 + 
  //But be careful with stings:  //But be careful with stings:
  cout << "​Please give me your full name: ";  cout << "​Please give me your full name: ";
- cin >> ​full_name+ cin >> ​fullName; 
- + cout << endl
- cout << ​full_name ​<< " really!!\n"; +  
- cin >> last_name+ cout << ​"Full name: " << fullName ​<< "?? really!!" ​<< endl
- cout << ​last_name ​<< " ​is the rest of it.\n"; +  
 + 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  // back to numbers
  // errors in cin  // errors in cin
- int interesting_number;+ int interestingNumber;
  cout << "I am expecting a number, try something unexpected:";​  cout << "I am expecting a number, try something unexpected:";​
- cin >> ​interesting_number+ cin >> ​interestingNumber;​ 
- cout << "\nI saw: " << ​interesting_number ​<< endl; + cout << endl
- cout << "What?\n\nMaybe ​this will help, give bad input here:\n"; + cout << "Interesting Number: " << ​interestingNumber ​<< endl; 
- cout << "​Please give me your favorite number:";​ + cout << "What?" << endl; 
- cin >> ​favorite_number+ cout << endl; 
- + cout << "​Maybe ​this will help, give bad input here:" ​<< endl
- cout << "\nHey, I know you, your favorite number is " << ​favorite_number ​<< 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!!  // I sure wish I could tell when input failed and do something about it!!
- +  
- + //system("​pause"​);​
- +
- +
- +
- system("​pause"​);​+
  return 0;  return 0;
 } }
 </​code>​ </​code>​
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