File handling

Pages: 12
Thanks for the assignment.

Have you learned about functions? Class methods? It appears to me that you're getting lost in the details of the code. Moving some of this stuff to functions can really help to keep things organized in your head and to build a complex program from simple parts.

For example, I'd start with a class to store a customer's data (like your original Applicant class) and add methods to read and write the customer:
1
2
3
4
5
6
7
class Customer {
public:
    string first, last;  // name
    int registration;  // registration code
    bool read(istream &);    // read from a stream
    bool write (istream &);  // write to a stream
};


Get this working with a main() program that simply reads from "from form.txt" and writes to "confirmed.txt". Obviously this isn't the whole program, but it's actually about half way there.

Now you have to deal with the store database. It would be inefficient read the file each time you want to search it, so you should probably read the file into memory first. Then you can search the in-memory copy. You could read it into a std::vector and search the vector each time, or, with a little more work, store it in a std::set, which will allow faster searches.

Either way, let's assume you store the data into some collection called storeData.

With these pieces, the main program becomes straightforward:

1
2
3
4
5
6
7
8
// Read storedatabase.txt into storeData
// Open the confirmed.txt and unconfirmed.txt output files.
// Open the form data input file
// For each customer in the input file (use Customer.read() to read the record)
    // If the customer is in storeData
        // write customer to confirmed.txt
    // else
        // write customer to unconfirmed.txt 


You can literally copy/paste these comments into main() and fill in the code under them.
Notice how I went about this
1. Figure out the data that you need (Customer class, collection of customers for store data)
2. Figure out what low level functions you think you'll need (read and write methods)
3. Pseudo-code the program in comments using the low level functions.
4. Write the code.

As problems get more complex it will take several iterations of steps 1-3 to get it right. For more complex problems, I'd also add one more step:
3a. Re-read the assignment, sentence by sentence. Make sure that every sentence is handled somewhere in your design.
Topic archived. No new replies allowed.
Pages: 12