| | |
Problem with loop
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
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.
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)
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; }
Last edited by lexusdominus; Jun 9th, 2009 at 7:59 am. Reason: i cant spell
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
I believe the preferred method of reading all the lines in a file is this:
C++ Syntax (Toggle Plain Text)
std::string line; while(getline(fin, line)) { //the current line is now in "line", handle it }
•
•
Join Date: Jun 2009
Posts: 21
Reputation:
Solved Threads: 2
•
•
•
•
I believe the preferred method of reading all the lines in a file is this:
C++ Syntax (Toggle Plain Text)
std::string line; while(getline(fin, line)) { //the current line is now in "line", handle it }
![]() |
Similar Threads
- Problem with do-while loop (C++)
- problem with loop (C)
- For loop problem (Java)
- problem with do...while loop (C++)
- problem with loop (C++)
- a for loop problem (Java)
- problem with MATLAB loop (Legacy and Other Languages)
- Problem with while loop... probably common (C++)
Other Threads in the C++ Forum
- Previous Thread: not sure how to fix this error msg
- Next Thread: Reading the value from a specific Memory Address
Views: 216 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





