Breaking out of a while loop if condition met

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Breaking out of a while loop if condition met

 
0
  #1
25 Days Ago
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
  1. while (p!=NULL)
  2. {
  3. if(p=="//")
  4. {
  5. break;
  6. }
  7. else
  8. {
  9. vec.push_back(p);
  10. p=strtok(NULL," ");
  11. }
  12. }

here is the full code
  1. // strings and c-strings
  2. #include <iostream>
  3. #include <cstring>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. vector<string> SplitString (string aString)
  10. {
  11. vector<string> vec;
  12. char * cstr, *p;
  13.  
  14. //string str ("Please split this phrase into tokens");
  15.  
  16. string str = aString;
  17.  
  18. cstr = new char [str.size()+1];
  19. strcpy (cstr, str.c_str());
  20.  
  21. // cstr now contains a c-string copy of str
  22.  
  23. p=strtok (cstr," ");
  24. while (p!=NULL)
  25. {
  26. // i presume a check must be done here
  27. vec.push_back(p);
  28. p=strtok(NULL," ");
  29. }
  30.  
  31. delete[] cstr;
  32.  
  33. return vec;
  34. }
  35.  
  36. int main ()
  37. {
  38. std::vector<std::string> vec = SplitString("Please split this phrase into tokens // this bit needs to be ignored");
  39.  
  40. for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
  41. {
  42. cout << vec.at(a) << '\n';
  43. }
  44. return 0;
  45.  
  46. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 457
Reputation: Grn Xtrm is on a distinguished road 
Solved Threads: 36
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #2
25 Days Ago
Have you tried using the following as the condition for the while loop
  1. while (p!=NULL && p!="//")
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.
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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training
 
0
  #3
25 Days Ago
Didn't work....i think i tried that already

I've also tried
  1. while (p!=NULL)
  2. {
  3. unsigned int pos = str.find("//", 0);
  4. if(pos != string::npos)
  5. break;
  6. else
  7. vec.push_back(p);
  8. p=strtok(NULL," ");
  9. }
but that doesn't output anything so i guess it's found the // and broke out of the loop
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC