Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

9.9 Ch 9 Program: Data visualization (C++) (1) Prompt the user for a title for d

ID: 3801686 • Letter: 9

Question

9.9 Ch 9 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

Here you go, the solution for your problem with all the specified formatting in the tables and the error conditions checked too.

#include <iostream>
#include <string.h>
#include <vector>
#include <iomanip>
using namespace std;

int main()
{
string title; //Title of the table
cout << "Enter a title for the data: ";
getline(cin, title);
cout << "You entered: " << title << endl;

string header1, header2; //Column headers declaration and input
cout << " Enter the column 1 header: ";
getline(cin, header1);
cout << "You entered: " << header1 << endl;

cout << " Enter the column 2 header: ";
getline(cin, header2);
cout << "You entered: " << header2 << endl;

string s; //String for data point input
vector<string> datastring; //String part of data point
vector<int> dataint; //Integer part of data point
int count=0, i=0; //Count to keep track of number of entries

cout << " Enter a data point (-1 to stop input): ";
getline(cin, s);

while(s.compare("-1")) //Checking for the exit condition
{
int numcomma=0, index; //Number of commas, index of the comma
int flag=1; //Flag to check if after comma there is integer or not
for(i=0; i<s.length(); i++)
{
if(s[i]==',')
{
index=i;
numcomma++;
if( !isdigit(s[i+1]) ) //Function to check integer
flag=0;
}
}
if(numcomma==0)
{
cout << "Error: No comma in string. ";
}
else if (numcomma>1)
{
cout << "Error: Too many commas in input. ";
}
else if(flag==0)
{
cout << "Error: Comma not followed by an integer. ";
}
else
{
int num=0, j;
for(j=index+1; j<s.length(); j++ ) //Storing the integer part of the data point
{
num=num*10 + s[j]-48;
}
string tmp;
tmp.assign(s,0,index); //Function to store the string part of the data point

datastring.push_back(tmp); //Storing the parts of the data point in the vector
dataint.push_back(num);

cout << "Data string: " << tmp << endl;
cout << "Data integer: " << num << endl;

count++; //Successful entry increments count
}
cout << " Enter a data point (-1 to stop input): ";
getline(cin, s);
}

//Formatting of the table
  
cout << endl << setw(33) << title << endl;
cout << setw(20) << left << header1 << "|" << setw(23) << right << header2 << endl;
for(i=0; i<44; i++)
cout << "-";
cout << endl;
for(i=0; i<count; i++)
{
cout << setw(20) << left << datastring[i] << "|" << setw(23) << right <<dataint[i] << endl;
}
cout << endl;

//Displaying the histogram
  
for(i=0; i<count; i++)
{
cout << setw(20) << datastring[i] << " ";
for(int j=0; j<dataint[i]; j++)
cout << "*";
cout << endl;
}
}