I need to wright an array 'membership' to a binary file
members.dat but am having no luck at all :O(
The function I am having trouble with is save_new_member()
Any comments/suggestions would be greatly appreciated!
Whole program is below as requested! It compiles in emacs, not sure about anything else.
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
enum Boolean {FALSE, TRUE};
const int STRING_LENGTH = 20;
const int MAX_MEMBER = 50;
// Structure for holding member information
struct member_type
{
// Add fields here for member records
char name[20]; // must include the null character
int age;
long number;
char gender[7]; // must include the null character
Boolean fees_paid;
};
void process_requests();
void obtain_member (member_type &this_member);
void display_member(member_type this_member);
void do_update();
void do_add();
void do_view();
void do_view_unpaid(); // added this function in P3
void do_count();
void save_new_list();
void read_old_list();
Boolean find_member(char members_name[STRING_LENGTH], int &member);
void do_find();
// The number of current members
int number_of_members = 0;
//Global variable to hold a array of members P3.
member_type membership[MAX_MEMBER];
void main()
{
read_old_list();
process_requests();
save_new_list();
}
void process_requests()
{
char command;
do
{
cout << endl;
cout << "a(dd), s(earch), v(view), p(view unpaid), u(update), f(inished): ";
cin >> command;
switch (command)
{
case 'a':
do_add();
break;
case 'v':
do_view();
break;
case 'p':
do_view_unpaid();
break;
case 'u':
do_update();
break;
case 's':
do_find();
break;
}
} while (command != 'f');
}
//reads in data for a club member from the keyboard and stores
//it in the record
void obtain_member (member_type &this_member)
/*
Function which prompts the user for and reads all
information for a new member.
PRE : This_member holds no new member information
POST : this_member holds the new member information
*/
{
char paid; // Temporary Variable
Boolean loop = TRUE; // While loop equals true, it will continue to loop
cout << "Name: ";
cin >> this_member.name;
cout << "Age: ";
cin >> this_member.age;
cout << "Phone Number: ";
cin >> this_member.number;
cout << "Gender: ";
cin >> this_member.gender;
do
{
cout << "Has this member paid their fees? y/n: ";
cin >> paid;
if (paid == 'Y' || paid == 'y')
{
this_member.fees_paid = TRUE;
loop = FALSE;
}
else if (paid == 'N' || paid == 'n')
{
this_member.fees_paid = FALSE;
loop = FALSE;
}
else
cout << "ERROR\n";
}
while(loop == TRUE);
}
void display_member(member_type this_member)
/*
This function displays the information on this_member
PRE : this_member holds the appropriate info
POST : information on this_members has been displayed
*/
{
// Check to see that appropriate data in structure is displaying itself correctly
cout << "Name: " << this_member.name << "\t";
cout << "Age: " << this_member.age << "\t";
cout << "Phone: " << this_member.number << "\t";
cout << "Gender: " << this_member.gender << "\t";
cout << "Fees Paid: ";
if (this_member.fees_paid == TRUE)
cout << "1 \n";
else if (this_member.fees_paid == FALSE)
cout << "0 \n";
}
void do_add()
/*
Function to add a new member to the current list of members.
PRE : number_of_members and the membership array hold the appropriate info
POST : a new member has been added to the array of members
*/
{
// This code puts the membership into the array at 0, and increments the number
// of members (refer to number_of_members being a counter)
obtain_member(membership[number_of_members]);
number_of_members++;
}
void do_view()
/*
This function displays the information on all members
PRE : number_of_members and membership hold the appropriate info
POST : information on all members has been displayed
*/
{
for(int i=0; i<number_of_members; i++)
display_member(membership[i]);
}
void do_count()
/*
This function calculates and displays how many members have not
paid their subscription
PRE : number_of_members and membership hold the appropriate info
POST : count of unpaid members has been displayed
*/
{
}
void save_new_list()
/*
This function updates the membership file by writing all member
info to the file.
PRE : membership holds number_of_members amount of member info
POST : the membership file has been updated and now holds the same
information as the membership array.
*/
{
int i=0;
ofstream outfile; // object name of data file is here
outfile.open("members.dat", ios:: binary || ios::out); // open the file members.dat
// check to see if the file is opened correctly
if (!outfile)
{
cout << "\nThe file was not successfully opened"
<< "\nPlease check that the file currently exists."
<< endl;
exit(1);
}
//THIS IS THE BIT IM HAVING TROUBLE WITH!!!!
//IS THIS CORRECT???????
outfile.write(&membership[i], sizeof(membership[i]));
i++;
outfile.close();
}
void read_old_list()
/*
This function reads in all of the member information from the
member file and stores it in the membership array. number_of_members
is assigned the total number of members read in. If the file cannot
be opened, the program should continue with number_of_members set to 0.
PRE :
POST : number_of_members holds the number of members stored
in the member file and the membersip array holds all member info.
*/
{
char ch;
int i=0;
ifstream infile; // object name of data file is here
infile.open("members.dat", ios:: binary || ios::in); // open the file members.dat
// check to see if the file is opened correctly
if (infile.fail())
{
cout << "\nThe infile was not successfully opened"
<< "\nPlease check that the file currently exists."
<< endl;
exit(1);
}
while( (ch = infile.peek()) !=EOF ) //check next character
{
infile.read(&membership[i], sizeof(membership[i]));
i++;
number_of_members++;
}
cout<< "Total members in array: " << number_of_members << endl;
}
Boolean find_member(char members_name[STRING_LENGTH], int &member)
/*
This function attempts to find a member with name members_name
in the list of known members. If the member is found then the
variable, member, is assigned the appropriate membership index
and the function returns true, otherwise the function returns
false.
PRE : number_of_members and membership hold the appropriate info
POST : if the member was found then member holds the members
membership index.
*/
{
return (FALSE);
}
void do_find()
/*
This function asks the user for the name of a member to look for
and tries to find that member in the list of known members. If
the member is found the index is displayed otherwise the user is
told that the member was not found.
PRE : number_of_members and membership hold the appropriate info
POST :
*/
{
char aname[STRING_LENGTH];
int index;
cout << endl;
cout << "Who are you looking for? ";
cin >> aname;
if (find_member(aname,index))
cout << "Found " << aname << " member index is " << index << endl;
else
cout << "Sorry - could not find " << aname << endl;
}
void do_view_unpaid()
{
/*
This function displays the information on all members
who have not paid their fee's
PRE : number_of_members and membership hold the appropriate info
POST : information on all unpaid members has been displayed
*/
int i;
for(i=0; i<number_of_members; i++)
// add an if statement here to only display the unpaid member
if (membership[i].fees_paid == FALSE)
// displays the data of that particular member
display_member(membership[i]);
}