Question

In: Computer Science

Please note that this problem have to use sstream library in c++ (1) Prompt the user...

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.

  • 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:

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 *

Solutions

Expert Solution

#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:


Related Solutions

note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is...
note: USE C++ WITH USER DEFINED FUNCTIONS AND RECURSIVE FUNCTION ONLY. No C++ library function is allowed. The game of “Jump It” consists of a board with n positive integers in a row, except for the first column, which always contains zero. These numbers represent the cost to enter each column. Here is a sample game board where n is 6: 0 3 80 6 57 10 The object of the game is to move from the first column to...
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
C code please (1) Prompt the user to enter a string of their choosing. Store the...
C code please (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and...
use c++ (1) Prompt the user for a string that contains two strings separated by a...
use c++ (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Print an error message if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts) Ex:...
use c++ (1) Prompt the user for a title for data. Output the title. (1 pt)...
use 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...
1. Write a program in C++ to find the factorial of a number. Prompt the user...
1. Write a program in C++ to find the factorial of a number. Prompt the user for a number and compute the factorial by using the following expression. Use for loops to write your solution code. Factorial of n = n! = 1×2×3×...×n; where n is the user input. Sample Output: Find the factorial of a number: ------------------------------------ Input a number to find the factorial: 5 The factorial of the given number is: 120 2. Code problem 1 using While...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the...
*******************In Python please******************* (1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be more shuttle flights and more...
Define a problem with user input, user output, -> operator and destructors. C ++ please
Define a problem with user input, user output, -> operator and destructors. C ++ please
Please Use the STL library for this question and Should be done in C++ and Provide...
Please Use the STL library for this question and Should be done in C++ and Provide screenshots of the OUTPUT Topic: Stack Infix to postfix conversion         Take Input mathematical expression in infix notation, and convert it into a postfix notation. The infix notation may or may not have round parenthesis. Postfix expression evaluation         Take Input mathematical expression in postfix form, evaluate it, and display the result The mathematical expression can contain Numbers as operands (numbers can be of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT