Computing.Net > Forums > Programming > Problem with a C++ program

Problem with a C++ program

Reply to Message Icon

Original Message
Name: earthstation
Date: June 27, 2007 at 00:30:23 Pacific
Subject: Problem with a C++ program
OS: Win XP Home SP2
CPU/Ram: AMD Durion 1.07GHz/ 378MB
Model/Manufacturer: Asus
Comment:

Hi,
I'm, hoping that someone can help me out with a problem i'm having. I'm making a program for management of small personal libraries. i recently ran into a bug while loading the program where it had to close. I'm not sure what exactly is causing the error so i was hoping someone could help me find and solve it. I think something in the function i'm using to load the library file (a text file) is causing it so here's the source for the function:
void loadLib()
{
//this function loads the library file into the global loadedLibrary array
ifstream openLibFile("library.txt");
int cellColumn, cellRow;

openLibFile.open();

if (! openLibFile)
{
cout << "\a !!Program Error!!" << endl;
cout << "Error Code: 426" << endl;
cout << "The program cannot find the library file!" << endl;
cout << "Please make sure the library file is in the application directory!" << endl;
cout << "If you require further assistance please contact the program" << endl;
cout << "developer at " << endl;
cout << "with the title \'Drake Error \', please include the error code above" << endl;
cout << "and a description of what you were doing before you received the error." << endl;
cout << "The program will start without a library file." << endl;
cout << "Press enter to continue.." << endl;
cin.get();
}
else
{
cout << "Loading library....." << endl;
while (! openLibFile.eof())
{
if (cellRow = 4)
{
getline(openLibFile, loadedLibrary[cellColumn][cellRow]);
lastColumn++;
cellColumn++;
cellRow = 0;
}
else
{
getline(openLibFile, loadedLibrary[cellColumn][cellRow++]);
; }
}
openLibFile.close();
}
}


Thanks in advance for your help
If you have any questions regarding this please ask.


Report Offensive Message For Removal

Response Number 1
Name: Razor2.3
Date: June 27, 2007 at 01:14:27 Pacific
Subject: Problem with a C++ program
Reply: (edit)

You never told us the error you're getting.

I'm not sure if your complier will allow it (I don't think it's suppose to), but you have a semicolon by itself on line 38.

35 else
36 {
37 getline(openLibFile, loadedLibrary[cellColumn][cellRow++]);
38 ; }
39 }
40 openLibFile.close();


Report Offensive Follow Up For Removal

Response Number 2
Name: earthstation
Date: June 27, 2007 at 07:35:09 Pacific
Subject: Problem with a C++ program
Reply: (edit)

The error i'm getting is windows telling me that the program encountered an error and needs to close. unfortunately that's about as detailed as it gets.
Removing the semi-colon seemed to fix that problem. Thanks for your help, i've got a new problem though, now its not finding the library file and is displaying the library not found error message, when the library file is in the directory with it.


Report Offensive Follow Up For Removal

Response Number 3
Name: earthstation
Date: June 27, 2007 at 07:54:09 Pacific
Subject: Problem with a C++ program
Reply: (edit)

nevermind, fixed that problem, now it's quitting while loading the library file again. same error message as before.


Report Offensive Follow Up For Removal

Response Number 4
Name: Razor2.3
Date: June 27, 2007 at 12:15:33 Pacific
Subject: Problem with a C++ program
Reply: (edit)

In that case, my guess would be that your globally declared array "loadedLibrary[][]" is too small for the amount of data used.


Report Offensive Follow Up For Removal

Response Number 5
Name: earthstation
Date: June 27, 2007 at 13:36:06 Pacific
Subject: Problem with a C++ program
Reply: (edit)

hmm, could be, but i don't think so, the library.txt file i'm loading only has information for about 20 books, the array is 500 indices long. could it be that it's too long?


Report Offensive Follow Up For Removal


Response Number 6
Name: Razor2.3
Date: June 27, 2007 at 15:01:53 Pacific
Subject: Problem with a C++ program
Reply: (edit)

Alright, alright. I'll take a closer look. (Read: Run it though my compiler.)

1: Neither cellColumn nor cellRow are initialized to 0.

2: I'm not sure of the version of fstream you're using, but mine forces me to declare the file to use in the open() statement. IE:
ifstream openLibFile;
openLibFile.open("library.txt");

3: if (cellRow = 4) should be if (cellRow == 4)

4: I'm not sure you can get away with two dimensional string arrays. You might need to make it a one dimensional array.

That's it for now.


Report Offensive Follow Up For Removal

Response Number 7
Name: Razor2.3
Date: June 28, 2007 at 21:05:12 Pacific
Subject: Problem with a C++ program
Reply: (edit)

Update to #4: It's not that you can can't use two dimensional string arrays. The problem is the string class overrides the [] operator. So, you'll probably end up with something that looks like this:
loadedLibrary[cellColumn][cellRow][0] = ...


Report Offensive Follow Up For Removal

Response Number 8
Name: earthstation
Date: July 4, 2007 at 23:37:19 Pacific
Subject: Problem with a C++ program
Reply: (edit)

So the string class is overriding essentially what is the
address of the cell? I'm not sure i completely understand


Report Offensive Follow Up For Removal

Response Number 9
Name: Razor2.3
Date: July 5, 2007 at 18:34:07 Pacific
Subject: Problem with a C++ program
Reply: (edit)

Yep. It overrides it, and points it to its own char[].


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Problem with a C++ program

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software