I'm making an address book for class and i'm stuck. The teacher helped with some of the code I already have. Sorry for the lack of comments. I need help with the function to add records and then save them. Thanks

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>

using namespace std;

typedef char small[15];
typedef char big[30];
    struct record
    {
        small first;
        small last;
        small phone;
        big address;
    }
record book[20];
void ReadBook(ifstream&, struct record[20], int&);
void ShowBook(ifstream&, struct record[20], int&);
void AddRecord(ifstream&, struct record[20], int&);
void SaveBook(ifstream&, struct record[20], int&);

int main()
{
    int length = 0;
    ifstream infile;
    infile.open ("MyBook.txt");
    char responce;
    ReadBook(infile, book, length);
    cout<<"Would you like to see your address book?\n";
    cout<<"Enter Y for yes and N for no\n";
    cin>>responce;
    if(responce == 'y' || responce == 'Y')
    {
        ShowBook(infile, book, length);
    }
    AddRecord(infile, book, length);
    SaveBook(infile, book, length);

    return 0;
}
void ReadBook(ifstream&, infile, struct record[20], int& length)
{
    char 1;
    int i = 0;

    infile.get(book[0].first, '\n');
    infile.get[1];
    length = 1;
    while(infile)
    {
        i++
        infile.get(book[i].first, '\n');
        infile.get(1);
    }
    length = i;
}
void ShowBook(ifstream&, struct record[20], int& length)
{
    for(int f = 0; f <= length; f++)
    {
        cout<<book[f].first<<endl;
    }
}
void AddRecord(ifstream&, struct record[20], int& length)
{
    //I'm confused how to add records
}
void SaveBook(ifstream&, struct record[20], int& length)
{
    //Also how to then save them back to the function
}

void AddRecord(ifstream&, struct record[20], int& length)

The first parameter is missing a variable name, e.g. ifstream& infile. But it doesn't make any sense to have an ifstream parameter to that function because AddRecord() creates a new record, not reading from a file.

Inside AddRecord() I assume you will want a loop to create up to 20 new books, so you would start out with a for loop that counts from 0 to 20. Inside the loop prompt for a book structure member's data and get keyboard input for it. If your instructions say the function is to add only one record then you don't need a loop -- use ther length paremter to determine which record to add, then increment length.

SaveBook() -- the first paremter is wrong -- you can't write to an ifstream object, you need ofstream for that. It's best not to pass it as a parameter, but create the ofstream inside the SaveBook() function. Is that function supposed to save all 20 books at the same time or only one book? That will make a difference how you write the function.

The two functions should be prototyped like below: Note that neither function uses an ifstream parameter. Also note that SaveBook() does not need a reference to numRecords because it won't make any changes to it.

void AddRecord(struct record[20], int& numRecords);
void SaveBook(struct record[20], int numRecords);

Oh okay, that makes a lot of sense, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.