Read this Entire Document Before Writing Any Code!!!;Problem Overview: What happens when your program asks the user to enter a...
Read this Entire Document Before Writing Any Code!!!;Problem Overview: What happens when your program asks the user to enter a number and they type something that;is all or partly non-numeric? Consider Program 1 below on the left. Five I/O sessions are shown with user input;underlined. Recall that the extraction operator (>>) reads from whitespace to whitespace in the input stream. When;extracting numeric values from the input stream the extraction operator stops when it encounters a non-whitespace;character that can?t be interpreted as part of a number. This can result in a failed input operation and it can leave;unprocessed characters in the stream. For example the ?a? in sessions #1 and #3, the ?34? characters in #2 and the ?- 5? in;session #5 are left unprocessed in the input stream.;One way to avoid the problems encountered with inputting values into numeric variables is to input them into string;variables (objects) using the getline function as shown in Program 2 on the right below. Unfortunately, while this fixes;the problem of leaving unprocessed characters in the input stream, it introduces the new problem that you can?t perform;arithmetic on string objects! You must compute the numeric equivalent of the string value in order to do arithmetic.;//PROGRAM 1;//Input numeric variable value directly;#include;using namespace std;int main();long num;cout num;cout << "You typed " << num << endl;return 0;I/O Session #1: Type a number: a;You typed -858993460;I/O Session #2: Type a number: 12 34;You typed 12;I/O Session #3: Type a number: 8a;You typed 8;I/O Session #4: Type a number: -5;You typed -5;I/O Session #5: Type a number: - 5;You typed -858993460;//PROGRAM 2;//Input numeric data into string object;#include;#include;using namespace std;int main();string num;cout << "Type a number:;getline(cin,num);cout << "You typed " << num << endl;return 0;I/O Session #1: Type a number: a;You typed a;I/O Session #2: Type a number: 12 34;You typed 12 34;I/O Session #3: Type a number: 8a;You typed 8a;I/O Session #4: Type a number: -5;You typed -5;I/O Session #5: Type a number: - 5;You typed - 5;Program 9 Specifications: You will create a set of C++ source code files that together will declare, implement and;test an InputNumber class. An InputNumber object will accept a user?s input from the console as a string of;characters. The string of characters will then be converted into a corresponding numeric value, or a flag will be set to;indicate that the string of characters did not represent a valid numeric value and so could not be converted. Your;InputNumber class should meet the specifications given in the UML diagram below.;After implementing the no-arg constructor, the input, appropriate setNum and accessor functions, you could write;Program 3 as follows;//PROGRAM 3;//Input numeric data using an InputNumber object;#include;#include;#include "InputNumber.h;using namespace std;int main();InputNumber num;cout << "Type a number:;num.input();cout << "You typed " << num.getInputString() << endl;if(num.validInputValue());cout << "What you typed has numeric value " << num.getValue() << endl;else;cout << "What you typed is not a valid numeric value. " << endl;return 0;HINT 1: New C++ string objects can be assigned the value of a null-terminated C-string char array as follows;cpp_String = c_char_array;But if you want to go the other direction you will have to use the page 239 C-string function strcpy and page 312;c_str function as follows: strcpy(c_char_array,cpp_String.c_str());HINT 2: You may find some of the old-style C-string functions on pages 237-239 useful. For example, you could use;atol (for ascii-to-long) to convert a C-string into an equivalent long value. (Note, if the C-string value is invalid;such as ?abc? the converted value will be 0. There is no way to tell if a value of 0 is based on a legitimate string;value or not.) To go the other direction you could use ltoa (for long-to-ascii similar to itoa on page 239), with;radix argument value 10 to indicate you want to convert long number into a base 10 string equivalent.;Alternatively, you might try the stringstream object approach on page 316 to convert a number into a string of;ascii characters.;HINT 3: Feel free to add private utility functions to the class to help you perform the public tasks if desired. For;example, I found it helpful to code a private bool function whose only job was to determine if the value of;inString represented a valid number or not. Expect to have to use a for-loop to visit each character in;inString to perform this task.;HINT 4: Recall from Chapter 9 that you may wish to first write the declaration, implementation and testing code all in one;file as shown in Listing 9.1 on page 286. Once your class is working correctly you can divide the code;appropriately among separate header, implementation, and testing files.;Language Usage Criteria;1 pt ? Find an appropriate use for the isdigit() character function described on page 173.;3 pts ? Your class declaration code is separated from your class implementation code and your class client testing code.;You will earn both points if the class declaration code is in a separate header file as shown in Listing 9.8 on page;295. If not in a separate file, the class declaration should be above the class implementation code and main() of;the client testing code.;1 pt ? Find an appropriate use for the atol() C-string function described on page 239.;1 pt ? Find an appropriate use for the ltoa() C-string function which is exactly like the itoa() function described on;page 239 and in HINT 2 above or use the stringstream technique shown on page 316;2 pts ? Find an appropriate use for at least two of the string functions and/or operators mentioned in Chapter 10.;3 pts ? The tester program contains a void displayNum function that accepts a reference to a InputNumber object;and any other parameters you may wish to use. Whenever your tester program wants to display the contents of;an InputNumber object it should call the displayNum function.;3 pts ? The tester program declares and uses an array of InputNumber objects. You may have other InputNumber;objects besides those in the array, but you are to demonstrate the ability declare and access effectively an array;of objects.;14 pts for Language Usage;Functionality Criteria;1 pt ? The no-arg constructor sets numValue to 0, inString to ?0? and validInput to true.;1 pt ? The second constructor calls the appropriate setNum function with the parameter value as the argument.;1 pt ? The third constructor calls the appropriate setNum function with the parameter value as the argument.;1 pt ? The getValue() function returns the current value of the numValue data field.;1 pt ? The validInputValue() function returns the current value of the validInput data field. NOTE: Not all ?get?;functions have to have ?get? as part of their name!;1 pt ? The getInString() function returns the current value of the inString data field.;1 pt ? The input() function uses the getline() function descibed on pages 315-316 to input what the user typed into a;local string object. It then calls the appropriate setNum function with the user?s input as the argument.;3 pts ? The commaString() function returns a string representation of the number containing commas if and where;appropriate. E.g. If inString is ?1000? commaString() returns a string with the value ?1,000?, if inString;is ?1000000? commaString() returns a string with the value ?1,000,000?, etc. Note: commaString() does;not change the value of inString!;3 pts ? The first setNum() function sets numValue to the newNum parameter?s value, inString to the string;equivalent of numValue, and validInput to true.;3 pts ? The second setNum() function sets inString to the strNum parameter value, and numValue to the best;possible numeric equivalent of inString. If there are no errors interpreting inString as a number;validInput is set too true, otherwise it is set to false.;Checkpoint Stub Code: Just assume that the strNum parameter represents a valid numeric value.;5 pts ? The InputNumber class code correctly identifies whenever inString is set to a non-numeric value as follows;? Any spaces at the beginning and ending of the string are ok, but?;? ?spaces in the middle of the string cause it to be invalid.;That is, ? 123 ? is a valid numeric string value, but ?1 23? is not.;? Aside from leading and trailing spaces, no other non-digit characters are valid in the string?;??except that ?-? (minus sign) is allowed to precede the first digit in the string.;That is, ?-45? is ok, but ?4-5? is not.;6 pts ? The client testing code exhaustively tests all of the functions of the InputNumber class. How do the functions of;the class behave when they are ?fed? good data? What about bad data?;Checkpoint Tester Code: Just use Program 3 below the InputNumber UML above.;27 pts for Functionality;Style Criteria;1 pt ? The first lines of each source code file contain comments that include the programmer name(s) and a brief;description of the purpose of the code in the file.;1 pt ? Use a consistent and appropriate indentation style as discussed in 2.12.3 on page 59. Follow the indentation style;used in the sample code throughout the book.;1 pt ? Use appropriate spacing and blank lines to enhance program readability as discussed in 2.12.3 on page 59.;1 pt ? All code is necessary to the proper functioning of the program, e.g. no unused variable declarations, no;unnecessarily complex code.;4 pts for Style;Total Lab 9 points possible: 45;Turn in;Send your header, implementation and client tester files as email attachments. The subject field of the email should say;CS 210 Lab 9. The message field of the email should contain the programmer name(s). If you are programming in a;team, be sure to cc your teammates.
Paper#73221 | Written in 18-Jul-2015Price : $27