944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 655
  • C++ RSS
Nov 6th, 2009
0

2 variables in for loop

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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; Nov 6th, 2009 at 12:51 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Nov 6th, 2009
0
Re: 2 variables in for loop
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.

C++ Syntax (Toggle Plain Text)
  1. for(int a=0, f=0; (a< vec.size() , f < vec.size()); a++, f++)
  2. {
  3. //do Stuff here
  4. }
Featured Poster
Reputation Points: 304
Solved Threads: 277
Posting Virtuoso
thines01 is online now Online
1,697 posts
since Oct 2009
Nov 6th, 2009
0
Re: 2 variables in for loop
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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; Nov 6th, 2009 at 4:57 pm.
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: C++ quick question, array
Next Thread in C++ Forum Timeline: How to display a variable menber of one class iside other





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


Follow us on Twitter


© 2011 DaniWeb® LLC