#include #include #include "stdlib.h" // for atoi using std::string; using std::getline; using std::cout; using std::cin; using std::endl; using std::istream; using std::streambuf; int ClearError(istream& isIn) // Clears istream object { streambuf* sbpThis; char szTempBuf[20]; int nCount, nRet = isIn.rdstate(); if (nRet) { // Any errors? isIn.clear(); // Clear error flags sbpThis = isIn.rdbuf(); // Get streambuf pointer nCount = sbpThis->in_avail(); // Number of characters in buffer while (nCount) { // Extract them to szTempBuf if (nCount > 20) { sbpThis->sgetn(szTempBuf, 20); nCount -= 20; } else { sbpThis->sgetn(szTempBuf, nCount); nCount = 0; } } } return nRet; } void main (void) { int i; string s1("This is the first string"); cout << s1 << endl << endl; s1 = "This is a test."; cout << s1 << endl << endl; s1.append(" What's up!"); cout << s1 << endl << endl; for (i=0; i < s1.length(); i++) cout << s1[i]; cout << endl; for (i=0; i < s1.length(); i++) cout << s1.at(i); cout << endl; s1.insert(5, "class "); cout << s1 << endl << endl; s1.erase(s1.find("W")); cout << s1 << endl << endl; int choice; while (choice != 5) { choice = 0; cout << "Please enter a choice (1-5; 5=quit): "; cin >> choice; ClearError(cin); if ((choice >= 1) && (choice < 5)) { cout << "Choice = " << choice << endl << endl; } } cout << "Done!" << endl << endl; }