Problem with loop

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Problem with loop

 
0
  #1
Jun 9th, 2009
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.


  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 638
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Problem with loop

 
1
  #2
Jun 9th, 2009
I believe the preferred method of reading all the lines in a file is this:

  1. std::string line;
  2.  
  3. while(getline(fin, line))
  4. {
  5. //the current line is now in "line", handle it
  6. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 21
Reputation: lexusdominus is an unknown quantity at this point 
Solved Threads: 2
lexusdominus lexusdominus is offline Offline
Newbie Poster

Re: Problem with loop

 
0
  #3
Jun 9th, 2009
Originally Posted by daviddoria View Post
I believe the preferred method of reading all the lines in a file is this:

  1. std::string line;
  2.  
  3. while(getline(fin, line))
  4. {
  5. //the current line is now in "line", handle it
  6. }
haha! thankyou!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 216 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC