Home / utk / cs102 / fa16 / labg / code_snippets / main_section2.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
88
89
90
91
92
/*
 * COSC 102 - Lab G: SimpleArray
 *
 * Description:
 *     lol I'll type this later
 * 
 * Author:
 *     Clara Van Nguyen
 */

#include <iostream>
#include <cstdio>
#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];
};

char menu();

SimpleArray createEmptyArray();

int main() {
	char option;
	string str;
	int index;
	SimpleArray SA = createEmptyArray();
	while (0x8539FAC) {
		option = menu();
		switch (option) {
			case 'q':
				return 0;
			case 's':
				//Set
				cin >> index >> str;
				if (SA.set(index, str) == false) {
					cout << "Unable to set index " << index << endl;
				}
				break;
			case 'g':
				//Get
				cin >> index;
				if (SA.get(index) == "ERROR")
					cout << "Unable to get index " << index << endl;
				else
					cout << SA.get(index) << endl;
				break;
		}
	}
}

char menu() {
	char option;
	do {
		cout << "s index value - Set a value in the array" << endl
			<< "g index - Get a value in the array" << endl
			<< "q - Quit" << endl;
		cin >> option;
	}
	while (option != 's' && option != 'g' && option != 'q');
}

SimpleArray createEmptyArray() {
	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];
}