nested for loop literal problem

consider the following loop(s)
1
2
3
4
5
6
7
8
for (int foo(0);foo<t_max;++foo)
{
   for (int goo?;goo<t_max;++goo)
        {
         do stuff
        }
    do stuff
}

I want goo to start each of its iterations with foo (outer loop)
So you would see the following:
1 (foo)
1->t_max (goo)

2 (foo)
2->t_max (goo)

3 (foo)
3->t_max (goo)

The problem is trying to initialize the variable in for loop goo.
e.g. for (int goo(foo); goo<tmax; ++goo) !!!error
{
do stuff
}

get an error that goo(foo) "cannot be used as a function".

How do I nest this loop? I could try a while loop, but I want to make sure Ii am not overlooking something with for loop. It seems you can only initialize the loop with literals and not symbols.

e.g. goo(5) // ok
x = 7; goo(x)//error
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

int main()
{
    const int N = 3 ;

    for( int foo = 0 ; foo < N ; ++foo )
    {
        std::cout << "foo == " << foo << '\n' ;
        for( int goo = foo ; goo < N ; ++goo ) std::cout << "\tgoo == " << goo << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "----------------------\n" ;

    for( int foo = 0 ; foo < N ; ++foo )
    {
        std::cout << "foo == " << foo << '\n' ;
        for( int goo{foo} ; goo < N ; ++goo ) std::cout << "\tgoo == " << goo << ' ' ;
        std::cout << '\n' ;
    }
    std::cout << "----------------------\n" ;

    for( int foo = 0 ; foo < N ; ++foo )
    {
        std::cout << "foo == " << foo << '\n' ;
        for( int goo(foo) ; goo < N ; ++goo ) std::cout << "\tgoo == " << goo << ' ' ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/d8ecdc4e9b5907ee
I had a bracket missing from the code that created errors with goo(foo), goo = foo

Thanks JL

1
2
3
4
5
6
7
8
9
10
11
12

int main()
{
	int max = 10;
	for (int foo = 0; foo<max; ++foo)
	{
	     {cout<<foo<<"  ";
	      cout<<endl;}
		for (int goo(foo); goo<max;++goo)
		  {cout<<goo<<"  ";}
		   cout<<endl;
	}
Last edited on
Topic archived. No new replies allowed.