Source of TestStuff21.cpp


  1: //TestStuff21.cpp

  2: //Tuesday, Mar 04, 2014

  3: 
  4: #include <iostream>

  5: #include <string>

  6: #include <iomanip>

  7: using namespace std;
  8: 
  9: int main()
 10: {
 11:     //int i;

 12:     //string name;

 13:     //cout << "Enter an integer and a name: ";

 14: 
 15:     //cin >> i;

 16:     //cin >> name; 

 17:     //cin.ignore(80, '\n');

 18:     //The above line removes the newline character from the

 19:     //input stream. This is necessary after the last read of a

 20:     //number or the reading of a string with cin, if you want

 21:     //the input stream to be empty (for a later pause, for example).

 22: 
 23:     //cout << "Twice the value you entered is " << 2*i << ".\n";

 24:     //cout << "Good-bye, " << name << ".\n";

 25:     //cout << "Press Enter to continue ... "; cin.ignore(80, '\n');

 26: 
 27: 
 28:     //int i;

 29:     //string name;

 30:     //cout << "Enter an integer and a name: ";

 31: 
 32:     //cin >> i;

 33:     //getline(cin, name); 

 34:     //cin.ignore(80, '\n');

 35:     //In this case the above line is not necessary to remove the

 36:     //newline character from the input stream, because getline()

 37:     //reads the rest of the line (including all whitespace) and

 38:     //then discards the newline character. So in this case the

 39:     //cin.ignore will cause your program to pause when you don't

 40:     //want it to do so.

 41: 
 42:     //Be sure to experiment with the above situations as we did

 43:     //in class to ensure you know how things work.

 44: 
 45:     //cout << "Twice the value you entered is " << 2*i << ".\n";

 46:     //cout << "Good-bye, " << name << ".\n";

 47:     //cout << "Press Enter to continue ... "; cin.ignore(80, '\n');

 48: 
 49:     //The following code illustrates that you can't add string

 50:     //literals in C++, but you can add a string class object and

 51:     //a string literal, or two string objects.

 52:     //cout << "\nHello, world!" << endl;

 53:     ////cout << "\nHello, " + "world!" << endl;

 54:     //cout << "\nHello, " "world!" << endl;

 55:     //string s1 = "\nHello, ";

 56:     //string s2 = "world!";

 57:     //cout << s1 + s2 << endl;

 58:     //cout << s1 + "world!" << endl;

 59: 
 60:     //string s1 = "Hello";

 61:     //string s2 = "Hello";

 62:     //string s3 = "hello";

 63:     //cout << boolalpha; //makes 1 and 0 appear as true and false

 64:     //cout << (s1 == s2) << " " << (s1 == s3) << endl;

 65: 
 66:     cout << endl;
 67:     cout << setw(4);
 68:     //setw(n) is a "manipulator" like endl, but requires including

 69:     //<iomanip>. It causes the next value output (and only the next

 70:     //value) to appear right-justified within n spaces.

 71:     cout << 6 << setw(4) << 7 << endl;
 72:     cout << setw(12) << "";
 73:     cout << "Hello, world!" << endl;
 74: }