In: Computer Science
use C++
(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<cstdlib>
#include<vector>
#include<iomanip>
using namespace std;
int main() {
/*
* Declaration of title as string type
*/
string title;
/*
* Declaration of datastring as string type of vector
*/
vector < string > datastring;
/*
* Declaration of datainteger as string type of vector
*/
vector < int > datainteger;
/*
* Declaration of header1,header2,value as string type
*/
string header1, header2, value;
/*
* Get the input entered by the user
*/
cout << "Enter a title for the data:" << endl;
getline(cin, title);
/*
* Display statement entered by the user
*/
cout << "You entered: " << title << endl <<
endl;
/*
* Get the input entered by the user
*/
cout << "Enter the column 1 header:" << endl;
getline(cin, header1);
/*
* Display statement entered by the user
*/
cout << "You entered: " << header1 << endl
<< endl;
/*
* Get the input entered by the user
*/
cout << "Enter the column 2 header:" << endl;
getline(cin, header2);
/*
* Display statement entered by the user
*/
cout << "You entered: " << header2 << endl
<< endl;
/*
* Get the input entered by the user until the user enters -1
*/
while (true) {
cout << "\nEnter a data point (-1 to stop input):" <<
endl;
getline(cin, value);
if (value == "-1") {
break;
} else {
/*
* Declare calculate commaCount as type of
integer
*/
int calculatecommaCount = 0;
int i = 0;
/*
* Iterate the loop
*/
while (i < value.length()) {
if (value[i] == ',') {
calculatecommaCount++;
}
i++;
}
if (calculatecommaCount == 0) {
cout << "Error: No comma in string." << endl;
} else if (calculatecommaCount > 1) {
cout << "Error: Too many commas in input." <<
endl;
} else {
/*
* find the string and integer from
datapoints
*/
string firstPoint = value.substr(0, value.find(","));
string secondPoint = value.substr(value.find(",") + 1,
value.length() - 1);
firstPoint.erase(0, firstPoint.find_first_not_of(' '));
secondPoint.erase(0, secondPoint.find_first_not_of(' '));
int countNumbers = 0;
for (int i = 0; i < secondPoint.length(); i++) {
if (secondPoint[i] >= '0' && secondPoint[i] <= '9')
{
countNumbers++;
}
}
/*Display statement*/
if (countNumbers == secondPoint.length()) {
datainteger.push_back(atoi(secondPoint.c_str()));
datastring.push_back(firstPoint);
cout << "Data string: " << firstPoint <<
endl;
cout << "Data integer: " << secondPoint <<
endl;
} else {
cout << "Error: Comma not followed by an integer" <<
endl;
}
}
}
}
/*
* Displaying the formatted output
*/
cout << right << setw(33) << endl;
cout << setw(20) << header1 << "|" <<
setw(23)<<right << header2 << endl;
cout << "-------------------------------------" <<
endl;
int j = 0;
while (j < datastring.size()) {
cout << setw(20) << datastring[j] << "|" <<
setw(23)<<right<< datainteger[j] << endl;
j++;
}
cout << endl;
cout << endl;
/*
* Displaying the output
*/
for (int k = 0; k < datastring.size(); k++) {
cout << setw(20) << right << datastring[k]
<< " ";
for (int j = 0; j < datainteger[k]; j++) {
cout << "*";
}
cout << endl;
}
system("pause");
return 0;
}
=========================================
Output:
====================================Thank You