Yurttas/PL/OOL/Cplusplus/F/02/06/00/e 09.cpp

From ZCubes Wiki
Jump to navigation Jump to search
 1/* Copyright(C) 2000
 2   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
 3
 4   Permission to use, copy, modify, and distribute this
 5   software and its documentation for EDUCATIONAL purposes
 6   and without fee is hereby granted provided that this
 7   copyright notice appears in all copies.
 8
 9   date   : January 1, 2000.
10   author : Salih Yurttas.
11
12   e_09.cpp
13*/
14
15
16#include <iostream>
17
18#include <string>
19
20#include <stdexcept>
21
22using namespace std;
23
24int main(int argc, char* argv[]) {
25
26  string t("tiger");
27  int index;
28
29  bool done;
30
31  do {
32    cout << "Enter an index: " ;
33    cin >> index;
34
35    cout << endl;
36
37    done = true;
38
39    try {
40      cout << "Char at position " << index << " is  " << t.at(index) << endl;
41    }
42    catch(out_of_range e) {
43      done = false;
44
45      cout << "Error : Index value for string out of range " << endl;
46      cout << e.what() << endl;
47      cout << endl;
48    }
49  }
50  while(!done);
51
52}