Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 861

D



  1. The following is one possible solution:
    0: // Ex1703.cpp
    1: #include
    2: using namespace std;
    3:
    4: int main()
    5: {
    6: char ch;
    7: cout << “enter a phrase: “;
    8: while ( cin.get(ch) )
    9: {
    10: switch (ch)
    11: {
    12: case ‘!’:
    13: cout << ‘$’;
    14: break;
    15: case ‘#’:
    16: break;
    17: default:
    18: cout << ch;
    19: break;
    20: }
    21: }
    22: return 0;
    23: }

  2. The following is one possible solution:
    0: // Ex1704.cpp
    1: #include
    2: #include
    3: using namespace std;
    4:
    5: int main(int argc, char**argv) // returns 1 on error
    6: {
    7: if (argc != 2)
    8: {
    9: cout << “Usage: argv[0] \n”;
    10: return(1);
    11: }
    12:
    13: // open the input stream
    14: ifstream fin (argv[1],ios::binary);
    15: if (!fin)
    16: {
    17: cout << “Unable to open “ << argv[1] <<” for reading.\n”;
    18: return(1);
    19: }
    20:
    21: char ch;
    22: while ( fin.get(ch))


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 861

Free download pdf