944,050 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 939
  • C++ RSS
Nov 3rd, 2009
0

Breaking out of a while loop if condition met

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Nov 3rd, 2009
0
Re: Breaking out of a while loop if condition met
Have you tried using the following as the condition for the while loop
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 3rd, 2009
0
Re: Breaking out of a while loop if condition met
Didn't work....i think i tried that already

I've also tried
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008

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: Array sort
Next Thread in C++ Forum Timeline: Overload Boolean Comparison Operators





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


Follow us on Twitter


© 2011 DaniWeb® LLC