-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab_program_8_11_data_visualization.cpp
More file actions
230 lines (159 loc) · 5.9 KB
/
lab_program_8_11_data_visualization.cpp
File metadata and controls
230 lines (159 loc) · 5.9 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'''
8.11 LAB*: Program: Data visualization
(1) Prompt the user for a title for data. Output the title. (1 pt)
Ex:
Enter a title for the data:
Number of Novels Authored
You entered: Number of Novels Authored
(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)
Ex:
Enter the column 1 header:
Author name
You entered: Author name
Enter the column 2 header:
Number of novels
You entered: Number of novels
(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in a vector of strings. Store the integer components of the data points in a vector of integers. (4 pts)
Ex:
Enter a data point (-1 to stop input):
Jane Austen, 6
Data string: Jane Austen
Data integer: 6
(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.
If entry has no comma
Output: Error: No comma in string. (1 pt)
If entry has more than one comma
Output: Error: Too many commas in input. (1 pt)
If entry after the comma is not an integer
Output: Error: Comma not followed by an integer. (2 pts)
Ex:
Enter a data point (-1 to stop input):
Ernest Hemingway 9
Error: No comma in string.
Enter a data point (-1 to stop input):
Ernest, Hemingway, 9
Error: Too many commas in input.
Enter a data point (-1 to stop input):
Ernest Hemingway, nine
Error: Comma not followed by an integer.
Enter a data point (-1 to stop input):
Ernest Hemingway, 9
Data string: Ernest Hemingway
Data integer: 9
(5) Output the information in a formatted table. The title is right justified with a setw() value of 33. Column 1 has a setw() value of 20. Column 2 has a setw() value of 23. (3 pts)
Ex:
Number of Novels Authored
Author name | Number of novels
--------------------------------------------
Jane Austen | 6
Charles Dickens | 20
Ernest Hemingway | 9
Jack Kerouac | 22
F. Scott Fitzgerald | 8
Mary Shelley | 7
Charlotte Bronte | 5
Mark Twain | 11
Agatha Christie | 73
Ian Flemming | 14
J.K. Rowling | 14
Stephen King | 54
Oscar Wilde | 1
(6) Output the information as a formatted histogram. Each name is right justified with a setw() value of 20. (4 pts)
Ex:
Jane Austen ******
Charles Dickens ********************
Ernest Hemingway *********
Jack Kerouac **********************
F. Scott Fitzgerald ********
Mary Shelley *******
Charlotte Bronte *****
Mark Twain ***********
Agatha Christie *************************************************************************
Ian Flemming **************
J.K. Rowling **************
Stephen King ******************************************************
Oscar Wilde *
'''
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
void printTable(string title, string header1, string header2, vector<string>& col1, vector<int>& col2) {
cout << setw(33) << title << endl;
cout << left << setw(20) << header1 << '|';
cout << right << setw(23) << header2 << endl;
cout << setfill('-') << setw(44) << "" << endl;
cout << setfill(' ');
for (unsigned int i = 0; i < col1.size(); i++) {
cout << left << setw(20) << col1[i] << '|';
cout << right << setw(23) << col2[i] << endl;
}
}
void plot(vector<string>& col1, vector<int>& col2) {
for (unsigned int i = 0; i < col1.size(); i++) {
cout << right << setw(20) << col1[i] << ' ';
for (int j = 0; j < col2[i]; j++)
cout << '*';
cout << endl;
}
}
int main() {
string title, header1, header2;
vector<string> names;
vector<int> nums;
string name;
int num;
string line;
istringstream iss;
cout << "Enter a title for the data:" << endl;
getline(cin, title);
cout << "You entered: "<< title << endl;
cout << endl;
cout << "Enter the column 1 header:" << endl;
getline(cin, header1);
cout << "You entered: "<< header1 << endl;
cout << endl;
cout << "Enter the column 2 header:" << endl;
getline(cin, header2);
cout << "You entered: "<< header2 << endl;
cout << endl;
cout << "Enter a data point (-1 to stop input):" <<endl;
getline(cin, line);
while (line != "-1") {
int commaCount = 0;
for (unsigned int i = 0; i < line.size(); i++) {
if (line[i]==',') {
commaCount++;
}
}
if (commaCount == 0)
cout << "Error: No comma in string." << endl;
else if (commaCount > 1)
cout << "Error: Too many commas in input." << endl;
else {
iss.clear();
iss.str(line);
getline(iss, name, ',');
iss >> num;
if (iss.fail()) {
cout << "Error: Comma not followed by an integer." << endl;
}
else {
names.push_back(name);
nums.push_back(num);
cout << "Data string: " << name << endl;
cout << "Data integer: " << num << endl;
}
}
cout << endl;
cout << "Enter a data point (-1 to stop input):" <<endl;
getline(cin, line);
}
cout << endl;
printTable(title, header1, header2, names, nums);
cout << endl;
plot(names, nums);
return 0;
}