help with do while loop

i want to display this (1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10) using the do while loop but i'm stuck.

1
2
3
4
5
6
7
  k = 0;
	do
	{

		cout << ","<< k;
		k = k + 1;
	}while ( k < 20);
You mean something like this?

1
2
3
4
5
6
7
8
        int k = 0;
	int j = 1;
	do
	{

		cout << j << ","<< k <<endl;
		k++;
	}while ( k < 20 );


By the way this can be done easy using 2D arrays.
Last edited on
no, i want it to be displayed this way
1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10
Well then remove the endl :p not that hard to figure out.

here just compile this whole thing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	int k = 0;
	int j = 1;
	do
	{

		cout << j << ","<< k <<" ";
		k++;
	}while ( k < 11 );

	return 0;
}
Last edited on
when i run that code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
	int k = 0;
	int j = 1;
	do
	{

		cout << j << ","<< k <<" ";
		k++;
	}while ( k < 11 );

	return 0;
}

the output is 1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10

i want the output to be 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9 1,10
Then start k off at 2 instead of 0.
k must set = to 0
Why?

The other option is to have
1
2
3
4
5
do
{
    cout << j << "," << k+2 << " ";
    k++;
} while (k+2 < 11);

but I don't see any reason why this is better than just setting the initial value of k to be 2.
does anybody know a different method to do it that uses the if command?
Okay, how about this?
1
2
3
4
5
6
7
do
{
    if (k < 2)
        continue;
    cout << j << "," << k << " ";
    k++;
} while (k < 11);
thank you
Topic archived. No new replies allowed.