CSc 220 Assignment 2

Over And Over


Sep 1
80 pts
Sep 16

Create a program which outputs a document based on the input described below. Read from standard input, and write to standard output. Standard input may be provided from a file using the < symbol on the command line, but do not attempt to open any file from inside your program. Read only from standard input.

Input Specification

The input file is a sequence of items separated by white space. “White space” means a run of one or more of any of the characters C++ standard input considers spaces, including space, tab and line break. (The standard input package skips white space between items it is reading, so this requirement should make it easier.)

Each item takes one of the following forms, and must perform the action described.

Print a single space.
Print a newline.
Where count is an unsigned integer. This should print a previous item count times. The previous item is the most recent one which is not a repeat count (this or the next). NOTE: If there are any non-digit characters following count, you should simply discard them.
Where count is an unsigned integer. This is similar to the previous item, but each time the item is printed, it will be printed with a preceding space.
anything else
Any word of input which does not fit any of the above patterns is simply printed.

Example Items

This just prints the string fred once.
fred +2
This prints fredfredfred, the first one from the original item, and the second from the repeat specification.
joe +2 +3
This prints joejoejoejoejoejoe.
zoo +s3
Prints zoo zoo zoo zoo'
+s +20
Will print 21 spaces.
+s +s10
Does the same.

For instance, the input

+s +2 +4 = +n +s +5 == +n +s +4 = +2 +n +s +3 = +3 +n +s +2 = +4 +s + +n +s +s == +2 +s ++ +n +s = +6 +s +++ +n = +4 +3 +s ++ +1 +n - +s6 +n
produces the output
= == === ==== ===== + ====== ++ ======= +++ ======== ++++ - - - - - - -
I will post some other example input when I get a chance.

Implementation Notes

You may assume the input is correctly formatted; you don't need to check for input errors.

Write in C++ and use the iostreams and strings packages.

You probably want to make a loop that reads one item for each iteration into a string. In the body of your loop, you must determine which of the specification forms above it matches, and take the required action. First, use subscripting or substring to determine if the first character is +. If so, you just need to print it. If it is a +, then check its second character (if any) to see what to do next.

To check for a number, it is sufficient to check the first character of the number to see if it is a digit. Use the isdigit function, for which you will need #include <cctype>. To convert the digits to an integer so you may use it in the loop index, use the stoi function. Its argument is any string, which includes substring expressions, of course.

There are many strings that start with + which do not match any of the specifications which start with +. They fall in the last category and should be printed.

You will also want to keep track of the last non-repeating specification so that a repeating spec can print it. You probably will want to declare a regular string variable to keep track of this. Whenever you print a (non-repeating) spec, also assign a copy to the variable. Then the last printing specification thing will always be available to the repeat.

Programming Style

Please indent your code properly. You should indent the body of a control constructs (such as if or while), and indent function bodies. The amount of the indent should be proportional to the nesting depth of code indented, and the amount should be consistent throughout the program. That is, any statement nested three levels should be indented three times as much as a statement indented one level, and the same as any other statement nested three levels.

Comment your code. The meaning of each variable should be clear; use good variable names and/or comment the declaration to be sure of this. Each program should have a comment at the top describing what the program does, and what its input must look like. Code bodies should be commented so that they are easy to understand by another programmer. A program is usually long-lived and will have to be understood by many persons other than its author. Good comments describe what's going on at a higher level than the code. For instance, this comment is remarkably useless:

// Increment counter ++in_count;
But this is better (presuming it is also correct):
// Keep track of the number of lines read. ++in_count;

The on-line code examples use an acceptable programming style.

Running Your Program

Your program should read from standard input (cin) and write to standard output (cout). Since you will not usually want to type the entire test input, you will need to supply it from a file. To do this, use the standard input redirection, <, to provide a file for standard input. Suppose we copy the first input example above input into a text file called testin.txt. If the program is compiled to an executable a2 (or a2.exe on Windows), you can run it like this:
[user@somehost cs220]$ a2 < testin.txt This is some input. Hope you really really really like it. It is nice!! Also, here is an sp. [user@somehost cs220]$
Here, the file testin.txt holds the input data, and the < directs cin to read it instead of the console.

This works fine with CodeBlocks on Windows. Create a text file containing the input data, either copied from this page or data you create yourself. Save the data file and your program in the same folder. You may compile your program from the GUI, then bring the command window up, and change to the folder where all the files are. Then run your program as shown above. Your program should not open any files. It should just read from cin, which can then be supplied from a file using the method described here.

One Possible Problem

In the past, some versions of the C++ compiler used with CodeBlocks on Windows have been missing the stoi function. This appears to be some defect in the Windows port of the compiler. If you run into this problem, add this code immediately above your main function:

int stoi(string s) { return atoi(s.c_str()); }
Also add an #include <cstdlib> at the top of your file. This will work around the problem by providing an equivalent.


When your program works and is properly formatted and commented, submit it online here.