Question

In: Computer Science

use c++ (4) Perform error checking for the data point entries. If any of the following...

use c++

(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

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#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


Related Solutions

4. Use the following information to perform the calculations in (a) – (c) below: Sales $101,000...
4. Use the following information to perform the calculations in (a) – (c) below: Sales $101,000 Administrative salaries 15,000 Indirect labor 12,000 Direct labor 35,000 Marketing expense 18,000 Materials purchased 25,000 Production machine depreciation 5,500 Materials inventory, Jan 1, 2014 35,000 Materials inventory, Dec 31, 2014 48,000 Finished goods inventory, Jan 1, 2014 19,000 Finished goods inventory, Dec 31, 2014 26,000 Work in process, Jan 1, 2014 18,000 Work in process, Dec 31, 2014 15,000 Required: a) Calculate Total Manufacturing...
- Error Checking - Use of Functions - Menu system - Array Processing
C language and that must contain the following: - Error Checking - Use of Functions - Menu system - Array Processing  
Use JQuery or any other JS library to perform the following – 1pt Create a JS...
Use JQuery or any other JS library to perform the following – 1pt Create a JS based menu – 3pt a. This can be as simple or complex as you want, from having JS change the colors of the links to flyout type menus or anything else you want. Create a slideshow – 3pt Create another interactive JS component (your choice) – 3pt a. When submitting the link to the page, describe what your choice was to ensure I see...
C# 1.Visual Studio .NET’s ___________ feature displays all the members in a class a.real-time error checking...
C# 1.Visual Studio .NET’s ___________ feature displays all the members in a class a.real-time error checking b.Quick Info c.outlined code d.Intellisense 2.An algorithm specifies the actions to be executed. True False 3.A(n) _________ occurs when an executed statement does not directly follow the previously executed statement in the written application. a.unordered execution b.transfer of control c.action state d.jump 4.The declarations and statements that compose the method definition are called the __________.    a.method body b.method header c.parameter list d.method name...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
The following is coded in C++. Please point out any changes or updates you make to...
The following is coded in C++. Please point out any changes or updates you make to the existing code with comments within the code. Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Also, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. (Note: the...
Please use the general journal entries to create the following with the data : Ledger /...
Please use the general journal entries to create the following with the data : Ledger / T-accounts showing posting of journal entries. Unadjusted Trial Balance Journal entries: Date Description debit Credit 1 Cash Common stock 1,500 1500 2 Properties Common stock 300 300 3 Loan Note Payable 1000 1000 4 Rent Expense Cash 16 16 5 Property Cash 600 600 5 Rent Expense Cash 24 24 6 Property Cash 200 200 6 Land Cash 400 400 7 Rent Expense Cash...
For the decision problem in Figure 6.1, use data tables to perform the following sensitivity analyses....
For the decision problem in Figure 6.1, use data tables to perform the following sensitivity analyses. The goal in each is to see whether decision 1 continues to have the largest EMV. In each part, provide a brief explanation of the results. a. Let the payoff from the best outcome, the value in cell A3, vary from $30,000 to $50,000 in increments of $2500. b. Let the probability of the worst outcome for the first decision, the value in cell...
Part A: Use the following data to determine the order with respect to reactant A in the reaction 3 A + 4 B → 2 C
Experiment    [A]      [B]   Initial Rate (M/s)         1         0.20    0.50        0.32         2         0.60    0.50        2.88         3         0.20    0.25        0.040 Part A: Use the following data to determine the order with respect to reactant A in the reaction 3 A + 4 B → 2 C Part B: Use the following data to determine the order with respect to reactant B in the reaction 3 A + 4 B → 2 C
Determine the point estimate of the population​ proportion, the margin of error for the following confidence​...
Determine the point estimate of the population​ proportion, the margin of error for the following confidence​ interval, and the number of individuals in the sample with the specified​ characteristic, x, for the sample size provided. Lower bound equals=0.689, upper bound equals=0.891, nequals=1200
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT