Program: Data visualization (C++) (1) Prompt the user for a title for data. Outp
ID: 3574999 • Letter: P
Question
Program: Data visualization (C++)
(1) Prompt the user for a title for data. Output the title. (1 pt)
Ex:
(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)
Ex:
(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:
(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:
(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:
(6) Output the information as a formatted histogram. Each name is right justified with a setw() value of 20. (4 pts)
Ex:
Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <string>
#include <locale>
#include <sstream>
#include <vector>
#include <iomanip>
using namespace std;
int main() // driver method
{
cout << "Enter a title for the data : "; // prompt
string title;
getline(cin, title); // read the data
cout << "You entered : " << title << endl; // prompt
cout << " Enter the column 1 header : "; // prompt
string c1Header;
getline(cin, c1Header); // read the data
cout << "You entered : " << c1Header << endl; // prompt
cout << " Enter the column 2 header : "; // prompt
string c2Header;
getline(cin, c2Header); // read the data
cout << "You entered : " << c2Header << endl; // prompt
vector<int> dataInt; // required local variables
vector<string> dataStr;
cout << " Enter a data point (-1 to stop input) : "; // prompt
string dp;
getline(cin, dp);
while(true) { // iterate over the data
if(dp == "-1") { // check for the input
break;
}
if (!(dp.find(",") != string::npos)) { // count the occurrences
cout << "Error: No comma in string." << endl; // prompt
cout << "Please Re-Enter.." << endl;
cout << " Enter a data point (-1 to stop input) : ";
getline(cin, dp);
} else {
size_t pos = dp.find(",", 0);
int count = 0;
for (int i = 0; i < dp.size(); i++) {
if (dp[i] == ',') {
count++;
}
}
if(count > 1) { // check for the count
cout << "Error: Too many commas in the input." << endl; // prompt
cout << "Please Re-Enter.." << endl;
cout << " Enter a data point (-1 to stop input) : ";
getline(cin, dp); // read the data
} else {
locale loc;
string ds = dp.substr(0, pos); // substring the data
cout << "Data string : " << ds << endl; // prompt
dataStr.push_back(ds); // add to the vectors
string di = dp.substr(pos + 2);
if (isdigit(di[0], loc)) { // check if is integer or not
int dataInteger;
stringstream(di) >> dataInteger;
cout << "Data integer : " << dataInteger << endl; // prompt
dataInt.push_back(dataInteger); // add the data
} else {
cout << "Error: Comma not followed by an integer." << endl; // prompt
cout << "Please Re-Enter.." << endl;
cout << " Enter a data point (-1 to stop input) : ";
getline(cin, dp);
}
}
}
cout << " Enter a data point (-1 to stop input) : "; // prompt
getline(cin, dp); // read the data
}
cout << endl;
cout << setw(33) << title << endl; // prompt
cout << c1Header << setw(20) << "|";
cout << setw(23) << c2Header << endl;
cout << "-------------------------------------------------------------" << endl; // prompt
for(int i = 0; i < dataInt.size(); i++) { // iterate to print the table
cout << dataStr[i] << setw(20) << "|";
cout << setw(23) << dataInt[i] << endl;
}
cout << endl;
cout << endl;
for(int j = 0; j < dataInt.size(); j++) { // iterate to print the histogram
cout << dataStr[j] << setw(20) << ": ";
for(int m = 0; m < dataInt[j]; m++) {
cout << "*";
}
cout << endl;
}
}
OUTPUT :
Enter a title for the data : Number of Novels authored
You entered : Number of Novels authored
Enter the column 1 header : Author Name
You entered : Author Name
Enter the column 2 header : Number of Novels
You entered : Number of Novels
Enter a data point (-1 to stop input) : jack, 5
Data string : jack
Data integer : 5
Enter a data point (-1 to stop input) : mary, 10
Data string : mary
Data integer : 10
Enter a data point (-1 to stop input) : cart, 38
Data string : cart
Data integer : 38
Enter a data point (-1 to stop input) : pink, k
Data string : pink
Error: Comma not followed by an integer.
Please Re-Enter..
Enter a data point (-1 to stop input) : -1
Enter a data point (-1 to stop input) : -1
Number of Novels authored
Author Name | Number of Novels
-------------------------------------------------------------
jack | 5
mary | 10
cart | 38
jack : *****
mary : **********
cart : **************************************
Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.