Source of TestStuff22.cpp


  1: //TestStuff22.cpp

  2: //Thursday, Mar 06, 2014

  3: 
  4: #include <iostream>

  5: #include <string>

  6: #include <iomanip>

  7: #include <cstdlib>

  8: using namespace std;
  9: 
 10: int main(int argc, char* argv[])
 11: {
 12:         //string s = "Hello, world!";

 13:         //cout << s << endl;

 14:     //

 15:     //Can use length() or size() for number of characters in s

 16:         //cout << s.length() << endl;

 17: 
 18:     //Can use [] or at() to get a single character

 19:         //cout << s[1] << endl;

 20:         //cout << s.at(1) << endl;

 21:     
 22:     //C++ string objects are not immutable

 23:         //s.at(1) = 'E';

 24:         //cout << s << endl;

 25: 
 26:     //Two versions of substr() 

 27:         //cout << s.substr(7) << endl;

 28:         //cout << s.substr(7, 5) << endl;

 29: 
 30:     //Here a is just an array of characters, *not* a C-string

 31:         //int a[] = {1, 2, 3};

 32:         //for (int i = 0; i < 3; i++)

 33:         //{

 34:         //        cout << a[i] << " ";

 35:         //}

 36:         //cout << endl;

 37: 
 38:     //Here s1 is also just an array of characters

 39:         //char s1[] = {'H', 'e', 'l', 'l', 'o'};

 40:         //for (int i = 0; i < 5; i++)

 41:         //{

 42:         //        cout << s1[i];

 43:         //}

 44:         //cout << endl;

 45:         //cout << s1 << endl;

 46: 
 47:     //But here s2 *is* a C-string, and so has a '\0' (null character)

 48:     //at the end, which allows all the legacy C-string functions in

 49:     //<cstring> to work properly with it

 50:         //char s2[] = "Hello";

 51:         //for (int i = 0; i < 5; i++)

 52:         //{

 53:         //        cout << s2[i];

 54:         //}

 55:         //cout << endl;

 56: 
 57:     //Can do this with a C-string, but not with an array of characters

 58:         //cout << s2 << endl;

 59: 
 60:         //Outputs all command-line parameters, one per line 

 61:         //for (int i = 0; i < argc; i++)

 62:         //{

 63:         //        cout << argv[i] << endl;

 64:         //}

 65: 
 66:         //atoi() converts a C-string to an integer.

 67:         //It comes from <cstdlib>.

 68:         cout << atoi(argv[1]) + atoi(argv[2]) << endl;
 69: }
 70: