In: Computer Science
Please note that this problem have to use sstream library in c++
(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.
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 <iomanip>
#include <sstream>
#include <cstdio>
#define MAX 50
using namespace std;
// Function to return number of comma available in the parameter
string data
int countComma(string data)
{
// Counter to store number of comma
int counter = 0;
// Loops till end of the string
for(int c = 0; c < data.length(); c++)
// Checks if current character is a comma character
if(data.at(c) == ',')
// Increase the counter by one
counter++;
// Returns the counter
return counter;
}// End of function
// Function to return the index position of comma
character
int getCommaPosition(string data)
{
// Loops till end of the string
for(int c = 0; c < data.length(); c++)
// Checks if current character is a comma character
if(data.at(c) == ',')
// Returns the loop variable value as found comma index
position
return c;
}// End of function
// Function to accept valid author name and number of
novels
// Returns the author name and number of novels by using pass by
reference (&name and &num) respectively
// Returns true if data is valid otherwise returns false
bool validateData(string data, string &name, int
&num)
{
// To store comma index position
int commaPosition;
// Calls the function to get number of commas
int comma = countComma(data);
// Checks if number of comma is equals to 0 then display error
message
if(comma == 0)
{
cout<<"\n Error: No comma in string.";
return false;
}
// Otherwise Checks if number of comma is greater than 1 then
display error message
else if(comma > 1)
{
cout<<"\n Error: Too many commas in input.";
return false;
}
// Otherwise valid name part
else
{
// Calls the function to get the comma position
commaPosition = getCommaPosition(data);
// Checks if the second position character after comma index
position is a not digit
// displays error message
if(!isdigit(data[commaPosition + 2]))
{
cout<<"\n Error: Comma not followed by an integer.";
return false;
}
// Otherwise valid
else
{
// Extracts the name part from data
name = data.substr(0, commaPosition);
string n = data.substr(commaPosition + 2);
// Extracts number of novels from the data
stringstream number(n);
// Converts to integer
number>>num;
return true;
}// End of else
}// End of outer else
}// End of function
// Function to accept author name and number of novels till user
not enters -1
void acceptData(string authorNames[], int numNovels[], int
&counter)
{
// To store the data entered by the user
string data;
// To store author name
string name;
// To store number of novels
int num;
// Loops till user enters -1
do
{
fflush(stdin);
// Accepts data
cout<<"\n Enter a data point (-1 to stop input): ";
getline(cin, data);
// Checks if data is equals to -1 then stop the loop
if(data == "-1")
break;
// Otherwise
else
{
// Calls the function to validate data
// If valid then extracts the author name and number of
novels
if(validateData(data, name, num))
{
// Stores the name at counter index position
authorNames[counter] = name;
// Stores the number of novels at counter index position
numNovels[counter] = num;
// Increase the counter by one
counter++;
}// End of if condition
}// End of else
}while(1);// End of do - while loop
}// End of function
// Function to display the data in tabular format
void showTabularFormat(string title, string heading1, string
heading2,
string authorNames[], int numNovels[], int counter)
{
cout<<endl<<endl;
// Displays the title
cout<<setw(30)<<title<<endl;
// Displays the heading
cout<<left<<setw(20)<<heading1<<setw(4)<<"
|"<<setw(20)<<heading2<<endl;
// Loops till number of authors
for(int c = 0; c < counter; c++)
// Displays the current author name and current number of
novels
cout<<left<<"
"<<setw(20)<<authorNames[c]<<setw(4)<<"|"<<setw(20)<<numNovels[c]<<endl;
}// End of function
// Function to display the data in histogram
void showHistogram(string authorNames[], int numNovels[], int
counter)
{
cout<<"\n\n ************** Histogram **************
\n\n";
// Loops till number of authors
for(int c = 0; c < counter; c++)
{
// Display the current author name in right justified
cout<<right<<setw(20)<<authorNames[c]<<"
";
// Loops till current number of novels
for(int d = 0; d < numNovels[c]; d++)
// Displays "*" character
cout<<"*";
cout<<endl;
}// End of for loop
}// End of function
// main function definition
int main()
{
// To store title and headings
string title, heading1, heading2;
// To store author names
string authorNames[MAX];
// To store number of novels for each author
int numNovels[MAX];
// To store number of records
int counter = 0;
// Accepts title
cout<<"\n Enter a title for the data: ";
getline(cin, title);
// Accepts heading 1
cout<<"\n Enter the column 1 header:";
getline(cin, heading1);
// Accepts heading 1
cout<<"\n Enter the column 2 header:";
getline(cin, heading2);
fflush(stdin);
// Calls the function to accept data
acceptData(authorNames, numNovels, counter);
// Calls the function to display data in tabular format
showTabularFormat(title, heading1, heading2,authorNames, numNovels,
counter);
// Calls the function to display data in histogram
showHistogram(authorNames, numNovels, counter);
return 0;
}// End of main function
Sample Output: