So bare with me on this one. I've been having a difficult time learning C++. For some reason, it's just not clicking with me. Could somebody point me in the right direction on this one? Thank you!
Objectives: reading from sequential access files with proper use of priming read, loop read, and test for end-of-file; one-dimensional arrays, defining functions.
In this program you will be writing two functions which you will compile and link with a test driver provided by your instructor. A short main() program is provided below if you would like to unit test your functions before linking with the test driver.
The first function, readData() accepts as arguments an input file name, a one-dimensional array of double, and an int to indicate the maximum number of triplets to read from the file into the array. For example, you will probably read values from the file three at a time. The first value will be loaded into the first element, the second value the second element, etc. Continue loading values in groups of three from the file into the array, but do NOT load values into the array if you are unable read a complete set from the file. So, you will need to be testing for End-Of-File, as well as input failure after each individual value is read in. Ultimately, the function must calculate and return an int code as follows:
Return
Code Message
0x0100 Error: Input file does not exist
0x0200 Input file exists, but is empty
0x0400 Input file has less than MAX triplets
0x0800 Input file has exactly MAX triplets
0x1000 Input file has more than MAX triplets
0x2000 Error: incomplete number triplet; EOF reached in middle of triplet
0x4000 Error: A value was attempted to be read in, but input failed
0x8000 Error: Input value not within proper range.
Your program must react correctly to each of the above conditions. In each case, make sure the proper return code(s) is/are returned. Combinations of codes are possible, i.e. if the input file exists, you will always report whether there were less than, more than, or exactly MAX number triplets. The last two codes will cause your program to return immediately with no further input file processing. However, in all cases, immediately prior to returning, add the number of triplets successfully read in to the Return Code.
readData() will open and read values from the input text file, and copy them into the provided array. The input file is expected to contain up to MAX triplets of floating point values. Your program should be written to only process up to MAX number triplets. When your program processes the required data, it must return the proper code(s) combined with the number of triplets processed. Though not required, it may be useful to display descriptive output messages during processing.
Example: Suppose the input file contains the values 1 2 3 4 5 6 -7 x 1 2 3 4. The return code would be: 0xc402, and your function might display the messages:
Error: Input value -7 not within proper range.
Error: invalid character 'x' read while reading triplet #3
Program aborting.
2 triplets processed.
IMPORTANT: It is assumed that all input text files end with a blank line. Without this assumption, you will not be able to get consistent, correct results.
You must also write a second function, displayDataTable() which accepts a one-dimensional array of double, followed by an int indicating the number of triplets contained within. e.g. 5 indicates that there are 15 values in the one-dimensional array. This function returns void.
For each set of three numbers in the array, it will:
Display the three numbers
Display the sum of the three numbers
Display a running total of all numbers read
For example, suppose the array contains the values 1 2 3 4 5 6. The output for your program would appear as:
First Second Third Running
Value Value Value Sum Total
1.00 2.00 3.00 6.00 6.00
4.00 5.00 6.00 15.00 21.00
Make sure to output appropriate table headers for your data, and organize the data into nicely formatted columns as shown above.
You could unit test your functions with the following program code, but do not submit this code as part of your assignment.
#include <iostream>
#include <fstream>
using namespace std;
int
main()
{
int retCode=0;
double darray[30];
(+0.00) Nice work.
(-2.50) Fails to return 1 error code correctly
(-5.00) Fails to return 2 error codes correctly
(-10.00) Fails to return 4 error codes correctly
(-5.00) Fails to report correct number of triplets processed
(-2.00) Numbers, sum, running total not displayed in organized tabular format
(-1.00) Incorrect sum for each number triplet
(-1.00) Running total not displayed or not calculated properly
Milestone submission
1. Create the two stubs for readData() and displayDataTable()
2. Implement readData() so that it opens the provided file, and reads and displays all values from the file until End-Of-File, or input failure occurs.
You are being provided with a pre-compiled main() program to link with your functions for testing. Your functions must compile, link, and run properly with this program for full credit. You will submit your properly compiling source file containing only your two functions.
Here is what you need to do:
Write the functions readData() and displayDataTable() as described above. Make sure all required header files are included at the top of your program file.
You may wish to write a main() program to test your function initially before proceeding with the following steps. If you do so, you must remove it or comment it out before continuing.
Since you will not be submitting the main() function, You will be compiling your program by doing the following:
Type your program code into Dev-C++
In Dev-C++, go to the Tools menu, and select Compiler Options to bring up a dialog box
Make sure the Compiler tab is selected, then check the box for "Add these commands to the linker command line."
In the textbox immediately below, type smfnMain.obj
Click the Ok button to close the dialog box
Save smfnMain.obj to the same folder where your C++ program file is. (Right-click, then Save Link As ... or similar.)
You can now Compile and Run your program using the test driver. If your program does not compile, then you probably have not defined your function correctly, or forgot to include all appropriate header files.
When running with the test driver, the program output will tell you if it is working correctly. Your program will be evaluated using the exact same test driver.
Submit only the file containing your function. You should not provide any test main() that you may have written for your own test purposes. When you submit your program on the class website, you should receive no compiler errors.