-
Simple program problem.
I am trying to make a hollow box. I have searched the forums and found the answers. But the answers do not help me understand how it works. I need help in the form of hints on how to make hollow squares. I am starting to learn c++ and I can't seem to get by the basics. Anywho....here is what I have so far.
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int row,col,rowcounter,colcounter,x,z;
cout <<"How many rows?" <<endl;
cin >>row;
cout <<"How many columns?"<<endl;
cin >>col;
for (rowcounter=1; rowcounter <= row; ++rowcounter)
{
cout <<"* ";
}
cout <<endl;
for (colcounter=2; colcounter <= col; ++colcounter)
{
cout <<"*" <<endl;
}
cout <<endl;
cin >> z;
return 0;
}
I think I am on the right track but I am missing something. I am re-reading the chapter (C++ How to Program: Deitel) and gonna keep trying to do this on my own.....Please don't just post an answer.
-
think about it fusion...you need to make a hollow box yet you're not printing any ('' ") empty spaces.
-
If I understand you correctly, you want the box to be hollow. So just the border should have *'s. I think also, you have rows and columns backwards.
Think about the problem. There are two common approaches to this. Iterate through the rows performing some action, or iterate through the columns performing some action. Let's say you go through it row by row. The first row is going to have asterik's all the way down because it is the top of your box. That was easy. The second row will have an asterik in first position (the left side) and in position of the last column (the right side). So there lies the problem. You need to print out an asterik, skip some spaces, then print out another. You said not to give you the answer so I'll let you look for how to do that on your own. The bottom of the box is identical the top of the box. Good luck and let us know if you have any problems.
-
-
Heh completely unrelated but I saw your location axon. My dad's office is in Mt. Prospect ;)
-
Another problem
Well I come across another problem. Here is what I have:
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int x, y, row, col, end;
cout <<"Enter amount of rows.\n";
cin >>row;
cout <<"Enter amount of coloumns.\n";
cin >>col;
for (x=1; x <=row; ++x)
{
cout <<"* ";
}
cout <<endl;
for (y=1; y <=col; ++y)
{
cout <<"*";
for (x=0; x<=row; ++x) // Middle rows
{
cout <<" ";
}
cout <<" *" <<endl;
}
for (y=1;y<=row; ++y)
{
cout <<"* "; // Bottom row
}
cin >>end;
return 0;
}
I am off by 2 spaces. Why?
-
Because you're printing "* " and " *" when you should only be printing the "*".
While I'm here, what's up with your for loops? Noone starts at 1. That's just ugly. ;)
[edit]
Code:
// top row
for( x = 0; x < width; x++ )
cout << "*";
cout << endl;
for( y = 0; y < rows; y++ )
{
cout << "*"; // left edge
for( x = 0; x < width-2; x++ );
cout << " "; // filler
cout << "*" << endl; //end the line
}
// bottom row
for( x = 0; x < width; x++ )
cout << "*";
cout << endl;
Is that what you were trying for?
[/edit]
Quzah.
-
Ok, its done and works like its supposed to now :) . I am glad that, not only did I figure out why my code was wrong, but I figured out how to make it work. It took me a while to realize how nested for loops work. It all makes sense now. When for loops are nested it completed the entire loop inside the loop.
So when I was wanting to make the middle I had to realize that I needed the for loop for the spaces to run inside the loop that made the colunms to fill in the spaces. Thanks for everyone that helped.