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.
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.
For instance, the input
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.
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:
The on-line code examples use an acceptable programming style.
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.
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:
When your program works and is properly formatted and commented, submit it online here.