create rectangle using single for loop

I am trying to create a rectangle using only one for loop but I am not sure how to go about this. The user is to enter a width and height and the output is a rectangle but in my code I can only use ONE for loop. How can this be done?
Keep a counter with the current line length and insert a newline when appropriate.
Either that or use the modulo operator to check when you need to change lines.
Thank you for your response. If I keep a counter with the line length, how do I know when to insert a newline without a nested for loop. Sorry, I am new at this and I just can't figure this out at all.
Every time the counter reaches "width"...
ok I am pretty sure I got it!

#include <iostream>
using namespace std;

int main()
{
int w, h, x, y;


cout<<"Enter width: ";
cin>>w;
cout<<"Enter height: ";
cin>>h;

x=w*h;
y=0;
while(y<x)
{
for(int i=1; i<=w; i++)
{
cout<<"*";
}
y=y+w;
cout<<endl;
}
return 0;
}
I doubt that will count. You're still using two nested loops, even if one of them is not a for loop.
My assignment says I can use only one FOR loop and nothing about while loops. It also says as a hint to multiply the height and width to get total number of characters in the rectangle. I figure since I am so new and this is a beginner class that I should get credit for this way. But just in case, how would you write the output?
Should be getting credit and getting credit are sometimes two different things.
Using modulo it could look somewhat like this:

1
2
3
4
5
for (int i=0;i<w*h;i++)
{
  cout << '*';
  if (i%w==w-1)cout << endl;
}
Topic archived. No new replies allowed.