Most of the codes are done but the results didn't align with the desired output on the attached handout section 6.2. I don't know where the problem is.
ECE244 Programming Fundamentals Fall 2020 Lab 3 : A Command Line Parser 1 Objectives The objectives of this assignment are for you to practice: (1) the use of C++ I/O streams, includ- ing error handling, (2) writing a simple class with constructors, accessors and mutators, and (3) dynamic allocation and de-allocation of one-dimensional arrays. You will do so through the design of a program that parses drawing commands from the standard input, displaying appropriate error messages if necessary, and by creating and maintaining objects that represent drawn shapes. 2 Problem Statement The assignment consists of two main parts. In the first part, you will write a command parser that provides a textual input interface to your program. The parser takes a sequence of commands as input. The commands create, delete modify and display shapes to be drawn on the screen. Each command consists of an operation keyword followed by arguments. The command and the arguments are separated by one or more spaces. Thus, the code you will write for this part of the assignment should take input from the standard input, parse it, verify that it is correct, print a response or error message and either create, delete or modify Shape objects that represent the shapes specified in the command. The command parser loops, processing input as long as input is available. In the second part of the assignment, you will implement a simple “database” of objects that stores the created shapes. To do so, you will implement a class called Shape that is used to store the shape properties. Further, you will create and maintain a dynamically allocated array of pointers to Shape objects to keep track of Shape objects created and deleted. In this assignment, you will not actually draw the shapes to the screen, just process the com- mands and maintain the database of the Shape objects. 3 String Streams In the lectures, you were introduced to the standard input stream (handled by cin) as well as user-created file streams (handled by ifstream objects that you create). There is one more useful type of user-defined streams, namely string streams. These streams come handy when processing input one line at a time, as you will do in this assignment. The rest of this section introduces you to string streams and how to use them. You will find that they are not that much different than using cin or ifstream. String streams allows the extraction of input from a string, as opposed to from the keyboard (cin) or a file (ifstream)1. The following example demonstrates how this may be done. 1String streams can be also used as output streams, but in this section, only their use for input is described. Page 1 of 12 1 #include
2 using namespace std; 3 #include 4 #include 5 6 int main () { 7 int anInteger; 8 string inputLine = "204 113 ten"; 9 10 stringstream sin (inputLine); 11 12 sin >> anInteger; // Extracts 204 13 if (sin.fail()) return (-1); 14 sin >> anInteger; // Extracts 113 15 if (sin.fail()) return (-1); 16 sin >> anInteger; // Extraction fails 17 if (sin.fail()) return (-1); 18 19 return (0); 20 } The #include on line 4 imports the definition of string streams, allowing it to be used in the example. The main function creates a string variable called inputLine on line 8 and initializes it to "204 113 ten". The declaration on line 10 creates a new string stream handler called sin (ala cin, but you can give it any other name). This stream is initialized from the inputLine string variables created and initialized earlier2. Once this is done, we can use the sin handler in the same way we use cin. We can extract an integer from the stream, as shown on line 12. The handler sin has the same set of flags that cin has. Thus, we can check if the extraction operation failed by invoking the method sin.fail(). In the example, the extraction succeeds and the value 204 is placed in anInteger. The same happens for the second extraction on line 14, which extracts 113. In contrast, the third extraction on line 16 fails, the value of anInteger is not affected and main returns with exit code -1. The above example is not very interesting because it extracts input from a string initialized by the program and has the foreknowledge that three integers are expected. More interesting is when we wish to extract input from a string provided by the user and we have no knowledge of how many extractions we have to do. The example below illustrates how to do this. In the example, the function getline() is used to read the input the user provides through the keyboard and places the entire stream received by cin into the string inputLine. This includes all the white spaces in the stream (see your lecture notes for details). It also appends an eof to the stream. Thus, while a cin stream may end with an Enter or an end-of-file (eof) a string stream always ends with an eof. In the example, we assume the user entered 204 113 10 as input. Thus, inputLine contains "204 113 10". The string inputLine is then used to build the string stream handler called sin on line 11. Subsequently, the while loop iterates until there is no more integers in a line. sin is used to extract 2A copy of the string variable is made inside the string stream. Thus, if inputLine changes after the string stream is created, the stream in sin does not change. Page 2 of 12 an integer from the input (the string), as shown on line 16. The first extraction succeeds and thus the fail and eof flags of sin are false. The integer (204 in our example) is printed to the standard output and the moreInput flag remains true. The next two iterations of the while loop extract the next two integers, 113 and 10, and prints them to the output. On the next iteration of the while loop, the extraction fails because the eof is encountered. Both the fail and the eof flags are set to true. The code checks for these flags as shown on lines 17 and 18. Since the eof flag is true, the moreInputs flag is set to false (line 19) causing the while loop to exit and the program to terminate. Since the program immediately exits after checking the eof flag, there is really no need to clear the flags of sin, as the comment indicates in the code on line 20. Thus, it is safe to remove this line from the code. 1 #include 2 using namespace std; 3 #include 4 #include 5 6 int main () { 7 int anInteger; 8 string inputLine; 9 10 getline(cin, inputLine); 11 stringstream sin(inputLine); 12 13 bool moreInput=true; 14 while (moreInput) { 15 16 sin >> anInteger; 17 if (sin.fail()) { 18 if (sin.eof()) { 19 moreInput = false; 20 sin.clear(); // Not necessary 21 } 22 else { 23 cout < "bad="" input\n";="" 24="" sin.clear();="" not="" necessary="" 25="" sin.ignore(10000,’\n’);="" again="" not="" necessary="" 26="" }="" 27="" }="" 28="" else="" cout="">< "the="" integer="" read="" is:="" "="">< aninteger="">< endl;="" 29="" }="" 30="" 31="" return="" (0);="" 32="" }="" now,="" let’s="" assume="" that="" the="" user="" provides="" "="" 204="" 113="" ten"="" as="" input.="" the="" first="" two="" extraction="" succeed="" as="" above.="" however,="" the="" third="" extraction="" fails="" because="" of="" the="" ten.="" the="" fail="" flag="" is="" set="" to="" true="" but="" the="" eof="" flag="" is="" set="" to="" false.="" the="" code="" then="" checks="" if="" the="" reason="" of="" failure="" is="" the="" eof="" (line="" 18),="" and="" this="" is="" not="" the="" case.="" a="" message="" is="" printed="" to="" instruct="" the="" user="" that="" the="" input="" is="" bad.="" when="" using="" page="" 3="" of="" 12="" cin="" the="" flags="" must="" be="" cleared="" (using="" cin.clear())="" and="" the="" input="" stream="" must="" be="" flushed="" using="" cin.ignore().="" however,="" with="" string="" streams,="" this="" is="" unnecessary="" since="" we="" can="" simply="" discard="" the="" input="" by="" reading="" another="" line="" from="" cin="" using="" getline()="" and="" then="" rebuilding="" the="" stream="" handler="" using="" the="" new="" input="" (not="" shown="" in="" the="" example).="" this="" automatically="" resets="" the="" flags="" and="" flushes="" the="" old="" input,="" replacing="" it="" by="" the="" new="" one.="" thus,="" the="" two="" calls="" on="" lines="" 24="" and="" 25="" are="" not="" really="" necessary="" and="" can="" also="" be="" removed="" from="" the="" code.="" the="" use="" of="" string="" streams="" is="" helpful="" when="" input="" must="" be="" processed="" one="" line="" at="" a="" time="" and="" the="" user="" is="" not="" allowed="" to="" break="" input="" across="" multiple="" lines="" of="" input,="" separated="" by="" enters.="" string="" streams="" allows="" your="" program="" to="" get="" the="" entire="" line="" of="" input,="" analyze="" it="" and="" decide="" if="" the="" line="" is="" valid="" or="" not.="" while="" this="" can="" be="" done="" using="" cin,="" it="" is="" more="" difficult="" since="" cin="" allows="" user="" input="" to="" be="" split="" into="" multiple="" lines.="" indeed,="" this="" is="" the="" case="" for="" this="" assignment="" and="" the="" skeleton="" of="" the="" main="" program="" (included="" with="" the="" lab="" release)="" shows="" a="" modified="" version="" of="" the="" above="" example.="" 4="" specifications="" it="" is="" important="" that="" you="" follow="" the="" specifications="" below="" carefully.="" where="" the="" specification="" says="" shall="" or="" must="" (or="" their="" negatives),="" following="" the="" instruction="" is="" required="" to="" receive="" credit="" for="" the="" assignment.="" if="" instead="" it="" says="" may="" or="" can,="" these="" are="" optional="" suggestions.="" the="" use="" of="" should="" indicates="" a="" recommendation;="" compliance="" is="" not="" specifically="" required.="" however,="" some="" of="" the="" recommendations="" may="" hint="" at="" a="" known-good="" way="" to="" do="" something="" or="" pertain="" to="" good="" programming="" style.="" your="" code="" will="" be="" marked="" subjectively="" for="" style,="" so="" it’s="" best="" to="" take="" the="" recommendations="" unless="" you="" have="" a="" good="" reason="" not="" to.="" example="" input="" and="" output="" for="" the="" program="" are="" provided="" in="" section="" 6="" for="" your="" convenience.="" they="" do="" not="" cover="" all="" parts="" of="" the="" specification.="" you="" are="" responsible="" for="" making="" sure="" your="" program="" meets="" the="" specification="" by="" reading="" and="" applying="" the="" description="" below.="" 4.1="" coding="" requirements="" 1.="" the="" code="" you="" will="" write="" shall="" be="" contained="" in="" only="" two="" source="" files="" named="" parser.cpp="" and="" shape.cpp.="" skeletons="" of="" the="" two="" files="" are="" released="" with="" the="" assignment’s="" zip="" file.="" the="" zip="" file="" also="" contains="" two="" .h="" files:="" globals.h="" and="" shape.h.="" these="" files="" are="" not="" to="" be="" modified="" in="" any="" way.="" modifying="" these="" files="" often="" results="" in="" a="" mark="" of="" 0="" for="" the="" assignment.="" however,="" you="" may="" make="" use="" of="" helper="" functions="" to="" split="" up="" the="" code="" for="" readability="" and="" to="" make="" it="" easier="" to="" re-use.="" these="" functions="" (and="" their="" prototypes)="" must="" be="" in="" one="" of="" the="" aforementioned="" two="" .cpp="" files.="" that="" is,="" you="" must="" not="" add="" any="" new="" .h="" or="" .cpp="" files.="" 2.="" input="" and="" output="" must="" be="" done="" only="" using="" the="" c++="" standard="" library="" streams="" cin="" and="" cout.="" 3.="" the="" stream="" input="" operator="">> and associated functions such as fail() and eof() shall be used for all input. C-style IO such as printf and scanf shall not be used. 4. Strings shall be stored using the C++ library