Hey. I have to say this is the first time ive been totally stumped by c++. I cant exit this loop. this code is from a function that takes a string filename, an int mode, and a char print.

It takes a textfile looking like this:

somesite.netinfoaboutsomesite
someothersite.orgdataonsomeothersite
yetanotherrandomsite.edurandomchars

and couts this:
somesite.net
someothersite.org
yetanotherrandomsite.edu

whilst returning this:
somesite.netsomeothersite.orgyetanotherrandomsite.edu

The problem im having is that It crashes when it exits a loop. Ive had similar problems when, for example, an array element that doesnt exist gets accessed. funny thing is, i can break out of the loop if i know how many entries/lines are to be read (but i dont). ive also tried changine the type of loop eg do while, for. heres my code. any help would be much appreciated.

ps sorry for my bad programming style im pretty new to it.

int linecounter = 0;
int counter = 0;
string temp = "";
string temp2 = "";
char f;
int j = 0;
string retdata;

if (mode == 3)
{
    ifstream data1( file.c_str() , ios::in);
         while (! data1.eof())
         {
         getline(data1, temp);   /*loop through lines, put them in temp*/
                do                              /*adds current char to temp2 string until it hits a '.'*/
                {
                  f = temp.at(j);
                  temp2.push_back(f);
                  j++;
                }while(f != '.');
                       
                if (f == '.')                  /*probs dont need this if but it doesnt hurt*/
                       {
                       for (counter = 0; counter < 3; counter++) /*gets the 3 subsequent chars after '.', stick them on temp2*/
                                    {                           
                                    f = temp.at(j);
                                    temp2.push_back(f);
                                    j++;
                                    }
                           retdata = retdata + temp2; /*add to string to be returned*/
                           if (print == 'p')cout << temp2 << "\n"; /*cout the string, if specified*/
                           temp2 = ""; /* reset all data ready for the next line in file*/
                           j = 0;
                           counter = 0; 
                           linecounter++; /*count how many lines are looped*/
                          
                       }
                system("pause"); /*for debugging purposes*/
         } /* on exit of the loop, program crashes */
if (print == 'p')cout << "File contains " << linecounter << " listings\n";
data1.close();
return retdata; 
}

I believe the preferred method of reading all the lines in a file is this:

std::string line;
	
while(getline(fin, line))
{
	//the current line is now in "line", handle it
}

I believe the preferred method of reading all the lines in a file is this:

std::string line;
	
while(getline(fin, line))
{
	//the current line is now in "line", handle it
}

haha! thankyou!

This question has already been answered. Start a new discussion instead.