Originally Posted by akhound
No that was not reading like I need to read into. I get the output great when I write the array to the file but I'm missing something in the read..... Since this is a class we have VERY limited commands to utilize.. I should be able to read in the file and assign each section to a variable then cin the variables to the array correct? I'm just missing the how to do it LOL....
As far as both C and C++ go, files are handled the same way as console I/O, with one additional requirement: you need to open the file first. In C++, the access is through objects of the iostream class (you'll cover objects later). When you start up a C++ program, there are a number of these objects created for you: cin, cout, cerr, and clog, which correspond to standard input, output, error, and log streams, respectively. If I have an output stream, such as cout, cerr, or an fstream object (which is derived from iostream, and is thus also an iostream object), I can use the << operator to output data to it. Aside from using the different object name, the code will be identical.
Obviously, there are certain things you can do with file streams that don't make sense for non-file streams, such as setting the position within the file, but you don't need to worry about that. Basically, all you need to do is open the file, then treat it the same way as cin. You don't need to read the file and then read from the variable with more << operators. While you can do that (with string streams), there's no reason to here. Try something like this:
using namespace std;
int main(void); // unnecessary, but I prototype everything, including main()
// above two lines could have been combined as:
// ifstream inFile("filename.txt");
string name; // much safer than C-style char arrays
name >> inFile;
expenses >> inFile;
expenses >> inFile;
cout << name << ' ' << expenses << ' ' << expenses;
You'll see a few things here. First, I don't use an array for the name; I use a string type. The string class lets you treat strings more naturally, and it's safer because it handles allocation of the memory for you, as well as having a bunch of built-in methods (functions that belong to the class) that do stuff like compute length, concatenate/assign strings, manipulate substrings, etc. Second, I can use the extraction operator, >>, directly on the ifstream object exactly the same as if it were cin.
There's no error checking in this program, but you can do a simple check like
// do whatever here...
Later on, you'll learn what to do if the input (from a file or the keyboard) isn't what you expected. When you read stuff in with <<, it sets a flag in the object that you can test to see if the input was valid, but that can be lesson #2.
I get the idea your instructor is telling you to "type this stuff in" without actually telling you how any of it actually works. My advice: read ahead, and try to understand the examples/handouts/scribbling on the overhead projector. I can't see your teacher getting upset if you use concepts ahead of time; that just shows you're willing to learn. Besides, there is an array buried in the guts of the string class anyways.
edit: I just took a look at your code; I didn't see the attachment when I first read your message. I see you need to read a series of records; that's pretty easy too; just change the code to declare an array of strings and a 2D array of double, and have a loop:
for(int x=0; x<NUM_RECORDS; x++)
inFile >> name[x] >> expenses >> expenses;
and that's it. Once you've done that, displaying it is easy. You can either change those other functions to use 'string ' instead of 'char ', or pass 'name[x].c_str()' to them (I told you there was a char array in there). I also see you've got #include <string>, so you may as well use the class.