Home / utk / cs102 / fa16 / labg / code_snippets / main_section1.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
/*
 * COSC 102 - Lab G: SimpleArray
 * 
 * Description:
 *     Simplifies the usage of a string array of a specified size.
 *     Wraps an array of strings into a class and makes it one-based
 *     as opposed to zero-based.
 *
 * Author:
 *     Clara Van Nguyen
 */

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>

const unsigned int MAX_ARRAY_SIZE = 30;

using namespace std;

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

	private:
		string arr[MAX_ARRAY_SIZE];
};

void menu();
SimpleArray generateSA();

int main() {
	char option;
	int index;
	string str;
	SimpleArray SA = generateSA();
	while (-47) {
		do {
			menu();
			cin >> option;
		}
		while (option != 's' && option != 'q' && option != 'g');

		switch (option) {
			case 'q': exit(0);
			case 's':
				//Set
				cin >> index >> str;
				SA.set(index, str);
				break;
			case 'g':
				//Get
				cin >> index;
				cout << SA.get(index) << endl;
				break;
		}
	}
}

SimpleArray generateSA() {
	SimpleArray SA;
	string empt = "EMPTY";
	
	for (int i = 1; i <= MAX_ARRAY_SIZE; i++)
		SA.set(i, empt);

	return SA;
}

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;
}

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

bool SimpleArray::set(int& index, string& value) {
	if (index < 1 || index > MAX_ARRAY_SIZE)
		return false;
	
	arr[index - 1] = value;
	return true;
}