| | |
2 variables in for loop
![]() |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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
and this is my code example
at the moment nothing is getting output or saved in a new vector so it is the for loop causing problems
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)
vec.push_back("!"); vec.push_back("="); vec.push_back("and"); vec.push_back("bee"); vec.push_back("="); vec.push_back("="); vec.push_back("car");
and this is my code example
C++ Syntax (Toggle Plain Text)
for(int a=0, f=0; a < vec.size(); a++) { f++; if(f < vec.size()) { rel1 = vec.at(a); rel2 = vec.at(f); vector<string> relation = JoinRelation(rel1, rel2); } }
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 11:51 am.
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 10
0
#2 Nov 6th, 2009
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.
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)
for(int a=0, f=0; (a< vec.size() , f < vec.size()); a++, f++) { //do Stuff here }
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
0
#3 Nov 6th, 2009
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
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
If it helps here is my code
I did this just to test and it gives the result i expect
C++ Syntax (Toggle Plain Text)
//for(int a=0, f=1; f < vec.size();f++, a++) //{ rel1 = vec.at(2); rel2 = vec.at(3); vector<string> relation = JoinRelation(rel1, rel2); //}
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)
for(int a=1; a < vec.size(); a++) { //if(f < vec.size()) //{ rel1 = vec.at(a-1); rel2 = vec.at(a); vector<string> relation = JoinRelation(rel1, rel2); //} }
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <vector> #include <cstring> #include <string> #include <sstream> #include <stdio.h> using namespace std; std::vector<std::string> JoinRelation(std::string rel1, std::string rel2 ) { std::string str; std::vector<std::string> relation; if((rel1=="<" && rel2=="=")||(rel1=="=" && rel2=="=")||(rel1=="!" && rel2=="=")||(rel1=="=" && rel2==">")) rel1.append(rel2); relation.push_back(rel1); return relation; } int main() { string rel1,rel2; vector <string> vec; vector <string> relation; vec.push_back("!"); vec.push_back("="); vec.push_back("and"); vec.push_back("bee"); vec.push_back("="); vec.push_back("="); vec.push_back("car"); for(int a=0, f=1; a < vec.size(); a++,f++) { //if(f < vec.size()) //{ rel1 = vec.at(a); rel2 = vec.at(f); vector<string> relation = JoinRelation(rel1, rel2); //} } cout << relation.size() << endl; for(int b=0; b < relation.size(); b++) { cout << relation.at(b) << endl; } return 0; }
Last edited by AdRock; Nov 6th, 2009 at 3:57 pm.
![]() |
Similar Threads
- calling modules using variables (Python)
- While loop, do-while loop and for (C)
- Building Variable for For() loop (C++)
- Need loop help (beginner) (VB.NET)
- Help With Transposition Cipher Loop ... (C++)
- Getting funky errors with a simple for loop, anyone care to help? (JavaScript / DHTML / AJAX)
- Double parsing variables? (PHP)
- help with program (C++)
- error checking help (C)
Other Threads in the C++ Forum
- Previous Thread: C++ quick question, array
- Next Thread: How to display a variable menber of one class iside other
Views: 421 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee input int lazy link linker list loop looping loops math matrix member memory newbie number object objects opengl operator output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference server sort spoonfeeding string strings struct student studio template templates text time tree undefined url variable vc++ vector video visual win32 window windows winsock wxwidgets





