looping problem

Hi all!

I am having problem while looping as below:

for (int openFile = 0; openFile < 31; openFile++)
{
ifstream readFile;
ofstream copyFile;

string fileObject[300];
string fileObject1[10]; // for temporary storage and future use

readFile.open(vectorObject.at(openFile)); //this works fine
if (!readFile)
{
cout << "Cannot open the file " << endl;
return 1;
}

//ignores the initial lines from the file

if(readFile.is_open())
{
for(int z = 0; z <9; z++)
getline(readFile, fileObject1[z]);
}

//program starts to crash from here... .. Coz if I put the cout function just above here.. it works fine.

int num =0;
while(!readFile.eof())
{
getline(readFile, fileObject[num]);
//cout << fileObject[a] << endl;
otherObject.push_back(fileObject[num]);
num++;
}

cout << vectorObject.at(openFile) << endl;
}

Please suggest me on this.!!!
Urgent PLs
Hi sanu,

First, please use code tags. The <> button under the format section just to the right of where you type in your post places code tags. Place the text of your code in between these code tags.

I notice that you haven't initialised the fileObject arrays so I am guessing that :

 
getline(readFile, fileObject1[z]); 


probably fails.

Have you tried using a debugger?

What is the purpose of your program? I am a little confused by the variable names openFile and fileObject1. Should these be FileCounter and StringArray?

It seems to me that you have C code in your C++ file. You are using the C function getline - there is also a C++ getline which operates on streams. In any case the second parameter is the number of chars to read and you have named that variable fileObject1[] which is really confusing.

The other thing is that you should put your declarations outside the for loop.

You should also open the file outside the for loop, do the processing in the loop, then close the file outside the loop. At the moment it looks like you want to open 31 files, then I get confused as to what you want to do.

Personally, I don't use single characters as variable names - it is much more meaningful to use a word that means something and gives the reader an idea on what the variable is really for.

Can you also provide the code for vectorObject and otherObject.

Another piece of advice (It's what I do):

Before you write any code, write a load of comments descibing what steps you are going take to complete your program. This is known as an algorithm or methodology. Now write your code after each comment. The comments serve as documentation in the code and help to enable clearer thinking.

Hope this has helped

Cheers TheIdeasMan
Thanks TheIdeasMan.. Superb.. i debugged it now...
Excellent, so what does your code look like now - just out of interest?

Cheers TheIdeasMan
Actually the code was correct ... but what makes it difficult was the limitation of the size of the vector array... what I did is just declared the size as minimum value and never tried to reset the initial value before another loop...

For actual code,, I cannot give you coz this is my assignment and need to submit first.

hope you can understand this..

Thank you for your great support and interest ..

Cheers!!!!
Topic archived. No new replies allowed.