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

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

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

Edited 7 Years Ago by AdRock: n/a

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.

for(int a=0, f=0; (a< vec.size() , f < vec.size()); a++, f++)
{
	//do Stuff here
}

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

//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

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);
	//}
}

If it helps here is my code

#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;
}

Edited 7 Years Ago by AdRock: n/a

This article has been dead for over six months. Start a new discussion instead.