C++ (1) Prompt the user for a title for data. Output the title. (1 pt) Ex: (2) P
ID: 3820374 • Letter: C
Question
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
// Histo.cpp : Defines the entry point for the console application.
//make small helper methods to reduce size of main
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
int main()
{
string title;
string header1;
string header2;
vector<int> values;
vector<string> names;
string str;
string number;
string datastring;
int datainteger;
cout << "enter title for data" << endl;
cin >> title;
cout << "you entered " << title << endl;
cout << "enter header for column1" << endl;
cin >> header1;
cout << "you entered " << header1 << endl;
cout << "enter header for column2" << endl;
cin >> header2;
cout << "you entered " << header2 << endl;
while (true)
{
cout << "Enter a data point in format name,value (-1 to stop input):" << endl;
cin >> str ;
if (str == "-1")
{
break;
}
std::string delimiter = ",";
int index1 = str.find(delimiter);
if (index1 == -1)
{
cout << "no comma" << endl;
continue;
}
datastring = str.substr(0,index1 );
string s = str.substr(index1+1, str.length() - 1);
names.push_back(datastring);
int index2 = s.find(delimiter);
if (index2 != -1)
{
cout << "too many commas" << endl;
continue;
}
number = str.substr(index1+1, str.length() - 1);
try
{
datainteger = stoi(number);
}
catch (exception e)
{
cout << "token after comma is not an integer" << endl;
}
values.push_back(datainteger);
cout << "data string" << datastring << endl;
cout << "data integer" << datainteger << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.