943,907 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 370
  • C++ RSS
Jun 9th, 2009
0

Problem with loop

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  1. int linecounter = 0;
  2. int counter = 0;
  3. string temp = "";
  4. string temp2 = "";
  5. char f;
  6. int j = 0;
  7. string retdata;
  8.  
  9. if (mode == 3)
  10. {
  11. ifstream data1( file.c_str() , ios::in);
  12. while (! data1.eof())
  13. {
  14. getline(data1, temp); /*loop through lines, put them in temp*/
  15. do /*adds current char to temp2 string until it hits a '.'*/
  16. {
  17. f = temp.at(j);
  18. temp2.push_back(f);
  19. j++;
  20. }while(f != '.');
  21.  
  22. if (f == '.') /*probs dont need this if but it doesnt hurt*/
  23. {
  24. for (counter = 0; counter < 3; counter++) /*gets the 3 subsequent chars after '.', stick them on temp2*/
  25. {
  26. f = temp.at(j);
  27. temp2.push_back(f);
  28. j++;
  29. }
  30. retdata = retdata + temp2; /*add to string to be returned*/
  31. if (print == 'p')cout << temp2 << "\n"; /*cout the string, if specified*/
  32. temp2 = ""; /* reset all data ready for the next line in file*/
  33. j = 0;
  34. counter = 0;
  35. linecounter++; /*count how many lines are looped*/
  36.  
  37. }
  38. system("pause"); /*for debugging purposes*/
  39. } /* on exit of the loop, program crashes */
  40. if (print == 'p')cout << "File contains " << linecounter << " listings\n";
  41. data1.close();
  42. return retdata;
  43. }
Last edited by lexusdominus; Jun 9th, 2009 at 7:59 am. Reason: i cant spell
Similar Threads
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
lexusdominus is offline Offline
84 posts
since Jun 2009
Jun 9th, 2009
1

Re: Problem with loop

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

C++ Syntax (Toggle Plain Text)
  1. std::string line;
  2.  
  3. while(getline(fin, line))
  4. {
  5. //the current line is now in "line", handle it
  6. }
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jun 9th, 2009
0

Re: Problem with loop

Click to Expand / Collapse  Quote originally posted by daviddoria ...
I believe the preferred method of reading all the lines in a file is this:

C++ Syntax (Toggle Plain Text)
  1. std::string line;
  2.  
  3. while(getline(fin, line))
  4. {
  5. //the current line is now in "line", handle it
  6. }
haha! thankyou!
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
lexusdominus is offline Offline
84 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: not sure how to fix this error msg
Next Thread in C++ Forum Timeline: Reading the value from a specific Memory Address





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC