General overview (In C++)
In this program you will be reading sales information from afile and writing out a bar chart for each of the stores. The barcharts will be created by writing out a sequence of *characters.
You will have to create input files for your program. You canuse a text editor such as Notepad or Notepad++ tocreate this. There may also be an editor in your IDE that you canuse. You can use the TextEdit program on macOSbut you will need to change the format to Make Plain Text.You will not be uploading these text files tozyBooks/zyLabs. The submit tests will be using their ownfiles. Make sure that some of your files contain a newline at theend of the last line of text in the file. You can do this byhitting the enter key after the last number of the last line in thefile. This will match the type of file usually created by programs.This is the type of file that is used for the zyBooks tests. Seethe description below on reading in files.
Reading in files
When reading data from a file you need to make sure you arechecking for end of file properly.
See Demo of file input and output of data inthis unit for an explanation of how to properly check for end offile.
The general method shown in the Gaddis text bookis:
ifstream inputFile;inputFile.open(\"input.txt\");int num;if (inputFile){ // the file opened successfully while (inputFile >> num) { // process the num value cout << num << endl; } inputFile.close();}else{ cout << \"The file could not be opened\" << endl;}
If you want to read in two values with every read you can simplyreplace inputFile >> num with something like inputFile>> num1 >> num2 as shown here:
while (inputFile >> num1 >> num2) { // process the num1 and num2 values cout << num1 << \" \" << num2 << endl; }
Text files are more complicated that they seem. Differentoperating systems handle text files differently. On Windows linesof text end with \r followed by \n. On Unix (or Linux) the linesend with just \n. On old Macs lines ended with \r but now use theUnix convention. The use of the >> operator takes care ofthese line ending issues for you.
But it is still more complicated than that. Most text files haveevery line ending with either \r \n (for Windows) or \n (forUnix/Linux and MacOS) but it is also possible to create a text filewhere the last line does NOT have the line ending characters. Theuse of the following code will work for all line endings even whenthe last line of text input does not end with any line endings.
if (inputFile >> num1 >> num2){ // executes only if the read worked}
or
while (inputFile >> num1 >> num2){ // executes while the read works}
There are other ways to test for end of file but you have tomake sure your code will work for all of the cases discussed above.It is STRONGLY recommended that you use the processoutlined above.
General overview (continued)
Your program will read in a file name from cin. It will thenopen the input file.
Your program must also open an output file calledsaleschart.txt. You will write the bar char headings anddata to this file.
Your program needs to have the following general flow:
prompt for the input file name with the prompt \"Enter input file name\"read in the file nameopen the input file for this file namedisplay a message if the input file does not open and quit your program open the output file (\"saleschart.txt\")display a message if the output file does not open, close the input file, and quit your programwhile (readFile into store number and sales for store if store number or sales are invalid display an appropriate error message (see below) else output bar chart for this store to the output file end ifend whileclose the input and output files
The processing loop will read the input data and process ituntil it gets and end of file indication from the file readoperation
Assuming you have read in valid data AND this is the first salesdata being processed your program should output some headings tothe output file before processing the data. The headings are asfollows:
SALES BAR CHART(Each * equals 5,000 dollars)
Note: Your program must not output the headings to theoutput file if all of the data in the input file is invalid, or ifthere is not any valid data in the input file.
You need to come up with a way of keeping track if this is thefirst valid read or not. .
Assuming you have valid data the processing will consistdisplaying the output
Once the loop has completed you need to close the inputfile.
If the input file could not be opened your program should outputan error message to cout. Assume the file we are reading in from iscalled sales.txt, and the file does not exist. The errormessage written to cout is:
File \"sales.txt\" could not be opened
The store number is of type unsigned int. Your program mustverify that the store number is in the range 1 to 99 (inclusive).If the store number is invalid display the following message:
If the store number is less than 1 or greater than 99 you needto output the following message to cout:
The store number xx is not valid
Where xx is the store number.
If the sales data is read in as a long long int. If the salesvalue is less than 0 you need to output the following message tocout:
The sales value for store xx is negative
Where xx is the store number.
Don't forget to close both files, if they were opened.
Write the bar chart information to the file.
You will be outputting a string of * characters where each *represents $5,000 in sales for that store. For each 5,000 in salesyou output one *. You do not round up the sales, so sales of$16,000 and sales of $16,999 would both output 3 * characters.
You will output the sales bar chart to the output file.
Assuming a store number of 9 and sales of $16,999. thedisplay function will write the following to the outputfile:
Store 9: ***
Note that the store width is 2 characters, so theoutput is:
Store yy: *******
The yy has a width of 2 even if the store number is 1through 9.
The format of the input file
The data in the input file is in the order store number followedby the store sales. There will be zero or more of these input pairsin the file.
Here is the contents of a sample input text file:
1 100002 250003 370004 290005 8000
Sample runs
Here is an example run. Assume the following input beingread in from cin:
sales.txt
Assume that the content of the file sales.txt are asfollows:
1 100002 250003 370004 290005 8000
The output (to file saleschart.txt) for this inputwould be:
SALES BAR CHART(Each * equals 5,000 dollars)Store 1: **Store 2: *****Store 3: *******Store 4: *****Store 5: *
You are reading from an input file and you are writing to anoutput file. Make sure you close both files after you are finishedusing them. You must do this in your program, you cannot just letthe operating system close the files for you.
In this lab. and some future labs, you will be creating anoutput file. There could be output to cout as well.
For tests where there is output written to an output file thecontents of the output file will determine if you passed that testor not. For cases where you have written out to cout the tests willcheck the output sent to cout.
Failure to follow the requirements for lab lessons canresult in deductions to your points, even if you pass thevalidation tests. Logic errors, where you are not actuallyimplementing the correct behavior, can result in reductions even ifthe test cases happen to return valid answers. This will be truefor this and all future lab lessons.
Expected output
There are eight tests. Each test will have a new set of inputdata. You must match, exactly, the expected output.Tests 2, 5, 6,and 7 check the output sent to cout. Tests 1, 3, 4, and 8 check theoutput sent to file saleschart.txt.
You will get yellow highlighted text when you run the tests ifyour output is not what is expected. This can be because you arenot getting the correct result. It could also be because yourformatting does not match what is required. The checking thatzyBooks does is very exacting and you must match it exactly. Moreinformation about what the yellow highlighting means can be foundin course \"How to use zyBooks\" - especially section \"1.4 zyLabbasics\".
Finally, do not include a system(\"pause\");statement in your program. This will cause your verification stepsto fail.
Note: that the system(\"pause\"); command runs the pausecommand on the computer where the program is running. The pausecommand is a Windows command. Your program will berun on a server in the cloud. The cloud server may be running adifferent operating system (such as Linux).