drawing a square with loops
Hi all,
Pretty simple task to do, but for some reason when run the square is a little off. The code I have done so far is:
Code:
#include <iostream>
#include <string>
#include <conio.h>
using std::cin; using std::endl;
using std::cout; using std::string;
int main()
{
const int cols = 10;
const int rows = 10;
for (int r = 0; r != rows; ++r)
{
int c = 0;
while (c != cols)
{
if (r == 0 || r == rows - 1 || c == 0)
{
cout << "*";
}
else if ( c == cols - 1)
{
cout << "*" << endl;
}
else
{
cout << " ";
}
++c;
}
}
getch();
return 0;
}
When run, the only problem is the top row has 11 * symbols and then a space of about 10 and then one more * symbol. Could anybody shed any light as to where I have gone wrong.
Thanks,
Darren.