Converting for loops into do while loops

I need help converting the for loops into do while loops. Not sure what I am doing wrong. The second code is what I am having trouble with.

This is the for loops code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
int main ()
{
	int number;
	cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
	cin>>number;
	if (number < 1)
        number = 1;
    else if (number > 15)
        number = 15;
	for (int i=0; i <number; i++)
	{
		for (int j=0; j <number; j++)
		{
			cout<<'X';
		}
		cout<<endl;
	}
system("pause");
return 0;
}


My attempt at do while loops code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main ()
{
	int number, i=0, j=0;
	cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
	cin>>number;
	if (number < 1)
        number = 1;
    else if (number > 15)
        number = 15;
	{
		i=1;
		do {i++;}
		while (i < number);
		j=1;
		do {j++;}
		while (j < number);
			{cout<<'X'<<endl;}
		cout<<endl;
	}
system("pause");
return 0;
}


Any help is appreciated. The second code works, however, it's not displaying the same output as the first code.
Last edited on
The do while syntax:
1
2
3
4
5
do
{
  //Things
}
while (/*condition*/);


So your example needs to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int number = 15;
int i = 0;
int j = 0;

do
{
  j = 0;
  do
  {
    cout<<'X'<<endl;
    j++;
  }
  while (j < number);

  cout  << endl;
  i++;
} while (i < number); // the while can go here too 


Note that variables made within the do brackets are not in scope within the while condition.
Last edited on
I tried that but it's not displaying the same output as the first source code. It's supposed to display a square using "X" whose sides are same as the number input by the user.

It's displaying this when I enter two

X
X

X
X


when it should look like this

XX
XX


How do I fix that? What line is the problem? Or is it my syntax again?

Here's the new program.
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
30
#include <iostream>
using namespace std;
int main ()
{
	int number;
	int i=0;
	int j=0;

	cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
	cin>>number;
	if (number < 1)
        number = 1;
    else if (number > 15)
        number = 15;
do
{
  j=0;
  do
  {
    cout<<'X'<<endl;
    j++;
  }
  while (j < number);
  cout << endl;
  i++;
} while (i < number);

system("pause");
return 0;
}


Last edited on
Line 20 should be:

cout<<'X';
What is wrong with the for loop? I would much prefer that over a mess of do loops.

Your original code is the standard idiom for doing what you want.
closed account (3qX21hU5)
@TheIdeasMan Im guessing his professor gave him the assignment of converting it to a do while loops, which I agree with you a for loops is better. But hey you never know with professors these days.
@Zereo

OK, no worries.

If I was his teacher, I would encourage him never to use do loops, unless absolutely necessary. But that is my personal pedantic opinion. I guess I was "brought up" that way - I find it odd that a lot of people opt for do loops as their first choice.

I would also mention to the OP, if you are going to use them, put the while part on the same line as the closing brace of the do.
It is being taught to us that you should use do while if you know you need to loop at least once. But to me a for loop can guarantee at least one pass just as easily, and is just an easier to read loop! But, there is scenarios that make sense to use other loops and would never discourage someone from at least learning how to use them! I would even encourage some one understand how GOTO loops work so they will see how nasty it gets rather than just trusting that it is bad!
ryancb06 wrote:
a for loop can guarantee at least one pass just as easily, and is just an easier to read loop!

Easier to read if that's the only type of loop you're familiar with. The idea is to use the appropriate tool for the task at hand, rather than try to use a single tool to do everything.

I have sometimes tried to use a screwdriver as a hammer, or pliers as wire-strippers, but it doesn't usually feel very satisfactory, even if it works.
Last edited on
I don't disagree at all.
Topic archived. No new replies allowed.