|
-
August 9th, 2010, 12:45 PM
#1
Unitialized memory help
Hey guys, can you tell me why my array isn't being initialized properly? At least, I think that's what's going on.
This is for a school assignment. Couple caveats: Can't use vectors, lists, etc. The only #include's allowed are iostream, fstream & string. No error-checking required, although I'll probably add it if I have time.
This compiles, but crashes when I grow the index array (member of books class) and try to assign values to the new book object.
<code>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
#define booklist "booklist.txt"
class book
{
public:
// Member functions
// Constructors
string isbn; // ISBN code
string bc; // Book code
string aln; // Author's last name
string afn; // Author's first name
string bt; // Book title
string y; // Year of publication
string price; // Price
// Display all fields.
void display()
{
cout << "Book code:\t" << bc << endl;
cout << "ISBN Number:\t" << isbn << endl;
cout << "Author's Last Name:\t" << aln << endl;
cout << "Author's First Name:\t" << afn << endl;
cout << "Book Title:\t" << bt << endl;
cout << "Year of publication:\t" << y << endl;
cout << "Price:\t\t" << price << endl << endl;
}
// Set all variables to empty values
void init()
{
aln.clear();
afn.clear();
bt.clear();
y.clear();
price.clear();
bc.clear();
}
};
class books
{
public:
// Member Functions
// Constructors
book* index;
int bookCount;
// Initialize the catalog as an array of size n
void initarray(int n)
{
index = new book[n];
bookCount = n;
for (int i=0;i<n;i++)
{
index[i].init();
}
}
// Displays all records
void displayAll()
{
for (int i=0;i<bookCount;i++)
{
index[i].display();
}
}
// Grow the catalog's array
void grow()
{
int n=bookCount;
book* index2=new book[n+1];
for (int i=0;i<n;i++)
{
index2[i]=index[i];
}
delete[] index;
book* index=new book[n+1];
initarray(n+1);
for (int i=0;i<n;i++)
{
index[i]=index2[i];
}
bookCount=n+1;
index[bookCount].bc=bookCount;
delete[] index2;
}
// Shrink the catalog's array, removing the record of number n
void del(int d)
{
int n=bookCount-1;
book* index2=new book[n];
int j=0;
for (int i=0;i<n;i++)
{
if (n==d)
{
j=1;
}
index2[i]=index[i+j];
}
book* index = new book[n];
for (int i=0;i<n-1;i++)
{
index[i]=index2[i];
}
bookCount=n-1;
delete index2;
}
};
void intro(); // Prototype declaration
int getOption(); // Prototype declaration
void loadfile(books&);
void addBook(books&);
string popTabdelim(string);
int main()
{
books catalog;
bool q=false;
loadfile(catalog);
intro();
do
{
int o=getOption();
switch (o)
{
case 1:
{
addBook(catalog);
break;
}
case 2:
{
break;
}
case 3:
{
break;
}
case 4:
{
catalog.displayAll();
break;
}
case 5:
{
break;
}
case 6:
{
//savecatalog();
q=true;
break;
}
}
}
while (!q);
return 0;
}
void intro()
{
cout << "\nWelcome to Jason Black's Book Cataloging system!\n\n";
}
int getOption()
{
int o; //Stores the user's choice.
cout << "Available options:\n";
cout << "\t1. Add a new book" << endl;
cout << "\t2. Find a book" << endl;
cout << "\t3. Delete a book" << endl;
cout << "\t4. Display all books" << endl;
cout << "\t5. Save changes" << endl;
cout << "\t6. Quit" << endl << endl;
cout << "Option:\t";
cin >> o;
cout << endl;
return o;
}
void loadfile(books& catalog)
{
string line;
int n=0;
ifstream bookfile;
bookfile.open(booklist);
while (!bookfile.eof())
{
getline(bookfile,line);
n++;
}
catalog.initarray(n);
catalog.bookCount=n;
bookfile.close();
bookfile.open(booklist);
for (int i=0;i<n;i++)
{
getline(bookfile,line,'\n');
catalog.index[i].bc=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].isbn=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].aln=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].afn=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].bt=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].y=line.substr(0,line.find('\t'));
line=popTabdelim(line);
catalog.index[i].price=line.substr(0,line.find('\t'));
}
}
string popTabdelim(string line)
{
int tabloc=line.find('\t');
string popped=line.substr(tabloc+1,line.length()-tabloc);
return popped;
}
void addBook(books& catalog)
{
catalog.grow();
book& b = catalog.index[catalog.bookCount];
cout << "Adding a new book.\n";
cout << "ISBN Number:\t";
cin >> b.isbn;
cout << "Author's Last Name:\t";
cin >> b.aln;
cout << "Author's First Name:\t";
cin >> b.afn;
cout << "Book Title:\t";
cin >> b.bt;
cout << "Year of Publication:\t";
cin >> b.y;
cout << "Price:\t\t";
cin >> b.price;
}
</code>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|