Hello,
this is my first C++ assignment so please bear with me. I need to translate a C program to C by making variables in structures private(no classes yet!) and putting public inline functions. There's a good chance that I have much more problems with my code than I'm asking right now, but I have 4 spots that I'm currently stuck in and can't access properly.
My structures:
Code:
struct Container
{
private:
int count;
char** lines;
int nlines;
public:
int getCount(){ return count;}
void setCount( ){ count = 0;}
int getLineCount(){ return nlines;}
char* getLine(int count){ return *(lines+count);}
int addLine(char* line){
*(lines + count++) = makeString(line);
return nlines;
}
void deleteFile()
{
int x = 0;
if (lines)
{
for(x = 0; x < nlines; x++)
free(*(lines + x));
free(lines);
}
//free(container); DON'T KNOW IF I NEED THIS!!
}
};
Code:
struct FileHandler
{
private:
FILE* fpointer;
struct Container* container;
public:
int countLines(FILE* fpointer){
int count = 0;
char line[ONEK];
if (fpointer != NULL)
{
while (!feof(fpointer))
if (fgets(line, ONEK, fpointer)) count++;
rewind(fpointer);
}
return count;
}
void initContainer(){
int count = countLines(fpointer);
container = (struct Container*)malloc(sizeof(struct Container));
container->setCount();
container->getLineCount.nlines = count; // Problem #1 (both this problem and the one below throws error "left of must contain class/struct/union
container->getLine.lines = (char**)malloc( sizeof(char*)*container->addLine->nlines ); // Problem #2
}
}
Code:
struct Inventory
{
private:
Book* books; //an array of books
int nbooks; //num of books in inventory
public:
int fillInventory(struct Container* cp){ int current = 0;
initInventory(cp->getLineCount());
if (cp != NULL)
{
for (current = 0; current < cp->getLineCount(); current++)
books->initBook( (char*)(books + current), *(cp)->getLine(current)); //NO IDEA MOST LIKELY WRONG!
}
return nbooks;
}
void deleteInventory(){
if (books)
{
int x;
for (x = 0; x < nbooks; x++)
books->deleteBook((int)books + x); // DON'T KNOW
free(books);
}
}
};
And in my main:
Code:
void main()
{
struct FileHandler fhandle;
struct Inventory inventory;
printf("Processing File from: %s\n", FILE_NAME);
if (fhandle.processFile(FILE_NAME))
{
printf("Filling Inventory....\n");
inventory.fillInventory(); //Problem #3 (it needs to take struct Container cp, should I just add it in main?)
inventory.showInventory();
printf("Cleaning up....\n");
inventory.deleteInventory();
deleteFile(fhandle.container); //Problem #4 (no idea how to access)
}
}
-Thank you for your time. Any tips or suggestions are greatly appreciated!