2 variables in for loop

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

2 variables in for loop

 
0
  #1
25 Days Ago
I am trying to increment 2 variables in a for loop and i want the loop to exit when the highest value variable is reached

My example a gets the first element of a vector and f gets the next. It needs to keep doing that until f reaches the last vector and a gets the first from last vector

If i take it out the loop and put values in the vec.at() i get a value

There are my values
  1. vec.push_back("!");
  2. vec.push_back("=");
  3. vec.push_back("and");
  4. vec.push_back("bee");
  5. vec.push_back("=");
  6. vec.push_back("=");
  7. vec.push_back("car");

and this is my code example

  1. for(int a=0, f=0; a < vec.size(); a++)
  2. {
  3. f++;
  4. if(f < vec.size())
  5. {
  6. rel1 = vec.at(a);
  7. rel2 = vec.at(f);
  8.  
  9. vector<string> relation = JoinRelation(rel1, rel2);
  10. }
  11. }

at the moment nothing is getting output or saved in a new vector so it is the for loop causing problems
Last edited by AdRock; 25 Days Ago at 12:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 92
Reputation: thines01 is an unknown quantity at this point 
Solved Threads: 8
thines01 thines01 is offline Offline
Junior Poster in Training
 
0
  #2
25 Days Ago
What else do you have affecting the value of "f"?
If both "f" and "a" increment at the same time, do you need two variables?
You could do it all in the same for loop.
The center element of a for loop is a boolean test and can have more than one test in it.
When the test returns false, the loop breaks.

  1. for(int a=0, f=0; (a< vec.size() , f < vec.size()); a++, f++)
  2. {
  3. //do Stuff here
  4. }
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
I don't know whay but my for loop is not working.

I did this just to test and it gives the result i expect
  1. //for(int a=0, f=1; f < vec.size();f++, a++)
  2. //{
  3. rel1 = vec.at(2);
  4. rel2 = vec.at(3);
  5.  
  6. vector<string> relation = JoinRelation(rel1, rel2);
  7. //}

so why does my for loop not work?

I want to get both vector elements until rel2 reaches the last vector element.

If i output the size of the relation vector i get a size of 0 but if i take it out of the for loop i get a size of 1

EDIT:

Just tried this which seems to make sense and compiles but i still get a vector size of 0. Also variables rel1 and rel2 both output values
  1. for(int a=1; a < vec.size(); a++)
  2. {
  3. //if(f < vec.size())
  4. //{
  5. rel1 = vec.at(a-1);
  6. rel2 = vec.at(a);
  7.  
  8. vector<string> relation = JoinRelation(rel1, rel2);
  9. //}
  10. }
If it helps here is my code

  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. #include <string>
  6. #include <sstream>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. std::vector<std::string> JoinRelation(std::string rel1, std::string rel2 )
  12. {
  13. std::string str;
  14. std::vector<std::string> relation;
  15.  
  16. if((rel1=="<" && rel2=="=")||(rel1=="=" && rel2=="=")||(rel1=="!" && rel2=="=")||(rel1=="=" && rel2==">"))
  17. rel1.append(rel2);
  18.  
  19. relation.push_back(rel1);
  20.  
  21. return relation;
  22. }
  23.  
  24. int main()
  25. {
  26. string rel1,rel2;
  27.  
  28. vector <string> vec;
  29. vector <string> relation;
  30.  
  31. vec.push_back("!");
  32. vec.push_back("=");
  33. vec.push_back("and");
  34. vec.push_back("bee");
  35. vec.push_back("=");
  36. vec.push_back("=");
  37. vec.push_back("car");
  38.  
  39. for(int a=0, f=1; a < vec.size(); a++,f++)
  40. {
  41. //if(f < vec.size())
  42. //{
  43. rel1 = vec.at(a);
  44. rel2 = vec.at(f);
  45.  
  46. vector<string> relation = JoinRelation(rel1, rel2);
  47. //}
  48. }
  49.  
  50. cout << relation.size() << endl;
  51.  
  52. for(int b=0; b < relation.size(); b++)
  53. {
  54. cout << relation.at(b) << endl;
  55. }
  56.  
  57. return 0;
  58. }
Last edited by AdRock; 25 Days Ago at 4:57 pm.
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