Question

In this assignment you are going to handle some basic input operations including validation and manipulation,...

In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes).

Functions that you will need to use:

getline(istream&, string&)
This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first parameter is typically cin, with the second parameter a string you want to read, like this:

string input;

getline(cin, input);

setw(int)
This looks like a function, but is really a stream manipulator. It allows you to specify how many characters the next output should be. This is useful when trying to line things up different outputs. (For more information look at section 12.1 Output formatting)

int stoi(string&)
This function takes a string, converts it to an integer and returns that integer. For example, the string "-24" returns an integer with the value of 24. If it is unable to convert (i.e. the string contains "Batman"), an exception of type invalid_argument is thrown, which you will need to catch if you want your program to continue.

try/catch
Not functions, but keywords, these are used to handle exceptions. Sometimes operations can fail to generate the correct results, while other times they may fail to generate ANY result. This could be due to incorrect input, going out of bounds of an array, and a variety of other cases. For more information, look at section 15.1 Exception basics.

Input Processing

Handling input will require the following steps:

  • Get input from the user in the form of a string (see the getline() function, above)
  • Check to see if that string has ONE comma in it
  • Split the string based on that comma into two parts
  • Convert the second part into an integer

Input Validation

For strings, validation is typically handled by checking to see if a string is equal to something, or perhaps whether it contains a certain character, or number of characters, etc.

For numeric values, though, while we typically validate whether its in a certain range or not, there is the possibility of something not being a number to begin with (for example, the user enters "dinosaur" when prompted to enter a number).

Assignment Details

(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. For the checking whether the entry after the comma is an integer or not, you will have to use try/catch blocks with the stoi function

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

Blind Tests (remaining points)

These are variations on the previous tests, with hidden input. Your code should work exactly the same in a variety of scenarios. For example, if you were to remove Mark Twain from the previous example, everything else should work just fine and the test would pass without issue.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <iomanip>

#include <string>

#include <vector>

using namespace std;

int main() {

string title, column1, column2, dataPoint;

cout << "Enter a title for the data:" << endl;

getline(cin, title);

cout << "You entered: " << title << endl;

cout << "\nEnter the column 1 header:" << endl;

getline(cin, column1);

cout << "You entered: " << column1 << endl;

cout << "\nEnter the column 2 header: " << endl;

getline(cin, column2);

cout << "You entered: " << column2 << endl<<endl;

vector<string> data;

vector<int> point;

while (true) {

cout << "Enter data point: " << endl;

getline(cin, dataPoint);

if (dataPoint == "-1")

break;

if (dataPoint.find(",") == string::npos)

cout << "Error: No comma in string." << endl;

else {

int count = 0;

for (unsigned int i = 0; i < dataPoint.size(); i++) {

if (dataPoint[i] == ',')

count++;

}

if (count > 1) {

cout << "Error: Too many commas in input." << endl;

cout << endl;

} else {

int data_int;

string data_str;

int pos = dataPoint.find(',');

try {

data_str = dataPoint.substr(0, pos);

data_int = stoi(dataPoint.substr(pos + 1));

data.push_back(data_str);

point.push_back(data_int);

cout<<"Data string: "<<data_str<<endl;

cout<<"Data integer: "<<data_int<<endl;

} catch (std::invalid_argument e) {

cout << "Error: Comma not followed by an integer" << endl;

cout << endl;

}

}

}

}

cout << setw(33) << title << endl;

cout << setw(22) << left << column1 << "|" << setw(23) << right << column2<< endl;

cout << "----------------------------------------------" << endl;

for (int i = 0; i < data.size(); i++) {

cout << setw(22) << left << data[i] << "|" << setw(23) << right << point[i]

<< endl;

}

cout << endl;

for (int i = 0; i < data.size(); i++) {

cout << setw(20) << data[i] << " ";

for (int j = 0; j < point[i]; j++)

cout << "*";

cout << endl;

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
In this assignment you are going to handle some basic input operations including validation and manipulation,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program: Data visualization

    5.10 LAB*: Program: Data visualization(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...

  • computer science . the programming language is python

    11.9 LAB*: Program: Data visualization(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...

  • Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table....

    Need help with this homework, and follow the bolded text required 7.10 LAB: Data Visualization (1) Write a function, get_data_headers(), to prompt the user for a title, and column headers for a table. Return a list of three strings, and print the title, and column headers. (2 pt) Ex: Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored Ex: Enter the column 1 header: Author name You entered: Author name Enter the column...

  • **C programming Language 3) Prompt the user for data points. Data points must be in this...

    **C programming Language 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 an array of strings. Store the integer components of the data points in an array of integers....

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • *In Python please***** This program will display some statistics, a table and a histogram of a...

    *In Python please***** This program will display some statistics, a table and a histogram of a set of cities and the population of each city. You will ask the user for all of the information. Using what you learned about incremental development, consider the following approach to create your program: Prompt the user for information about the table. First, ask for the title of this data set by prompting the user for a title for data, and then output the...

  • 6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

    6.6 Warm up: Parsing strings (Python 3) (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) Report an error 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...

  • Python 9.13 LAB: Warm up: Parsing strings (1) Prompt the user for a string that contains two strings separated by a comm...

    Python 9.13 LAB: Warm up: Parsing strings (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) Report an error 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...

  • Warm up: Parsing strings

    5.9 LAB: Warm up: Parsing strings(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, AllenJill , AllenJill,AllenEx:Enter input string: Jill, Allen(2) Report an error 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:Enter input string: Jill Allen Error: No comma in string. Enter input string: Jill, Allen(3) Extract the two words from the input string...

  • Hello, Could you please explain how I would complete this program with input validation to ensure...

    Hello, Could you please explain how I would complete this program with input validation to ensure that an error message will not appear if the user enters something other than an integer that is not 1-100 and later if they enter anything other than yes and no? Here is the program: Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly promt the user to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT