#include <iostream>
usingnamespace std;
int main ()
{
int number;
cout<<"Enter a positive integer no greater than 15: "<<" "<<endl;
cin>>number;
if (number < 1)
number = 1;
elseif (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;
}
#include <iostream>
usingnamespace 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;
elseif (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.
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.
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?
#include <iostream>
usingnamespace 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;
elseif (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;
}
@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.
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!
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.