| | |
2 variables in for loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 12:51 pm.
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
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 4: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
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





