Home / utk / cs102 / fa16 / labg / code_snippets / main_section3.cpp
Directory Listing
main_section1.cpp
main_section2.cpp
main_section3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * COSC 102 - Lab G: SimpleArray
 *
 * Description:
 *     I'll type this later.
 * 
 * Author:
 *     Clara Van Nguyen
 */

#include <iostream>
#include <string>

using namespace std;

const unsigned int MAX_ARRAY_SIZE = 30;

class SimpleArray {
	public:
		bool set(const int &, const string &);
		string get(const int &) const;

	private:
		string arr[MAX_ARRAY_SIZE];
};

void menu();

SimpleArray setAllEmpty();

int main() {
	int index;
	string str;
	char choice;
	SimpleArray SA = setAllEmpty();
	while (1) {
		do {
			menu();
			cin >> choice;
		}
		while (choice != 's' && choice != 'g' && choice != 'q');
	
		switch (choice) {
			case 'q':
				return 0;
			case 's':
				//Set
				cin >> index >> str;
				SA.set(index, str);
				break;
			case 'g':
				//Get
				cin >> index;
				cout << SA.get(index) << endl;
				break;
		}
	}
}

void menu() {
	cout << "s index value - Set a value in the array" << endl
	     << "g index - Get a value in the array" << endl
	     << "q - Quit" << endl;
}

SimpleArray setAllEmpty() {
	SimpleArray SA;
	for (unsigned int i = 0; i < MAX_ARRAY_SIZE; i++)
		SA.set(i + 1, "EMPTY");
	
	return SA;
}

bool SimpleArray::set(const int& index, const string& value) {
	if (index < 1 || index > MAX_ARRAY_SIZE)
		return false;

	arr[index - 1] = value;
	return true;
}

string SimpleArray::get(const int& index) const {
	if (index < 1 || index > MAX_ARRAY_SIZE)
		return "ERROR";
	else
		return arr[index - 1];
}