| | |
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; 25 Days Ago at 12:51 pm.
•
•
Join Date: Oct 2009
Posts: 92
Reputation:
Solved Threads: 8
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.
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 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
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; 25 Days Ago 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 |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





