| | |
Breaking out of a while loop if condition met
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I have a string which i can break down into tokens but what i want it to do is if it reaches a '//' it breaks out of the loop so no more tokens are added
I have tried using this but it doesn't work
here is the full code
I have tried using this but it doesn't work
C++ Syntax (Toggle Plain Text)
while (p!=NULL) { if(p=="//") { break; } else { vec.push_back(p); p=strtok(NULL," "); } }
here is the full code
C++ Syntax (Toggle Plain Text)
// strings and c-strings #include <iostream> #include <cstring> #include <string> #include <vector> using namespace std; vector<string> SplitString (string aString) { vector<string> vec; char * cstr, *p; //string str ("Please split this phrase into tokens"); string str = aString; cstr = new char [str.size()+1]; strcpy (cstr, str.c_str()); // cstr now contains a c-string copy of str p=strtok (cstr," "); while (p!=NULL) { // i presume a check must be done here vec.push_back(p); p=strtok(NULL," "); } delete[] cstr; return vec; } int main () { std::vector<std::string> vec = SplitString("Please split this phrase into tokens // this bit needs to be ignored"); for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a) { cout << vec.at(a) << '\n'; } return 0; }
0
#2 25 Days Ago
Have you tried using the following as the condition for the while loop
Then get rid of the if/else and just make the code currently after the else, the text of the while loop. It seems to me this is what you want the loop to do. Please correct me if I'm wrong.
C++ Syntax (Toggle Plain Text)
while (p!=NULL && p!="//")
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#3 25 Days Ago
Didn't work....i think i tried that already
I've also tried
but that doesn't output anything so i guess it's found the // and broke out of the loop
I've also tried
C++ Syntax (Toggle Plain Text)
while (p!=NULL) { unsigned int pos = str.find("//", 0); if(pos != string::npos) break; else vec.push_back(p); p=strtok(NULL," "); }
![]() |
Similar Threads
- ADVANTAGES of using while and do-while loop in C++ (C++)
- While loop, do-while loop and for (C)
- Random Number Problem For Lottery Program (Visual Basic 4 / 5 / 6)
- Factoring Code Question: (C++)
- breaking out of multiple loops (C)
- Breaking out of a Loop (Java)
- Loop...without the loop (Java)
Other Threads in the C++ Forum
- Previous Thread: Array sort
- Next Thread: Overload Boolean Comparison Operators
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





