Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi, I'm really new at C++. but I want to create a program that will output a hollow rectangle made of *.
*********
* *
* *
*********
I want the program to check to make sure the width or height is not larger than 25,
and values for height and width have to be greater than 2. I want the program to check to make sure these values are correct, and keep prompting the user for values until the correct values are entered. Finally, I want the program to be able to ask the user if they want to build another rectangle, and if they chose yes, it would start over. I'm really confused about how to tackle this...If anyone could help or guide me, I'd more than appreciate it! Thanks :)

You need to learn how to do this by yourself.
However, I had some time to kill.
#include <iostream>
using namespace std;
void rect(int a,int b)
{
for(int i=0;i<a;i++){for(int j=0;j<b;j++)cout<<"*";
cout<<"\n";}
}int main (void) {
int width,length;
do{
width=length=0;
cout<<"Enter the width(25 max: negative to
exit) =";
cin>>width;
if(width>0 and width<26)
do
{
cout<<"Enter the length(25 max : negative
to exit) =";
cin>>length;
if(length>0 and
length<26)rect(length,width);
}while(length>26 or length == 0);
cout<<"\n\n";
}while(width<26 and length> -1 and width > -
1);
return 0;
}

Thanks Jason. I am a bit confused though! I just purchased this C++ book and I'm only on chapter 4, loops...How can you do it using void main instead of void rect? I'm not that far yet :)

Thanks...I can do the a solid rectangle now, but am having trouble on how to make it hollow with blank spaces...If you could help, I'd more than appreciate it.

Lets be honest, the reason you want to create this program is because it is a homework assignment. Here is my policy on that:
http://www.computing.net/programming/wwwboard/forum/7407.html
Out of the kindest of my heart, I decided to help out. Here is the code that does what you are looking for. However, I left an error (or two+) in there that you will need to figure out to make it work for ever situation. I also did not include the check to see if it is within the 25 range that you wanted... this is fairly trivial. Here's the code:
<pre>
#include <iostream>using namespace std;
int main (void)
{
int rows;
int cols;
while (rows >= 0)
{
cout << "Enter rows: ";
cin >> rows;
cout << "Enter columns: ";
cin >> cols;int c, r;
for (r = 1; r <= rows; r++)
{
if (r != 2 && r != rows-1)
{
for (c = 1; c <= cols; c++)
{
cout << "*";
}
}
else
{
cout << "*";
for (c = 2; c <= cols-1; c++)
{
cout << " ";
}
cout << "*";
}
cout << endl;
}
}
return 0;
}
</pre>
IR

try this out. normally I would modulate this (split it into functions), but it appears that you aren't on that chapter yet :)
/////////////////////////////////////////////
#include <iostream.h> // for cout, cin
#include <ctype.h> // for toupper()const int MAX_WIDTH = 25;
const int MAX_HEIGHT = 25;int main(int argc, char** argv)
{
int nWidth, nHeight;
int nStar, nSpace;
char cChoice;do
{
// get the width
do
{
cout << "Please enter the width (1 - " << MAX_WIDTH << "): ";
cin >> nWidth;
}
while(nWidth < 1 || nWidth > MAX_WIDTH);// get the height
do
{
cout << "Please enter the height (1 - " << MAX_HEIGHT << "): ";
cin >> nHeight;
}
while(nHeight < 1 || nHeight > MAX_HEIGHT);// draw upper leg
for(nStar = 0;nStar < nWidth;nStar++)
{
cout << '*';
}
cout << endl;// draw left and right legs
for(nStar = 0;nStar < (nHeight - 2);nStar++)
{
cout << '*';for(nSpace = 0;nSpace < (nWidth - 2);nSpace++)
{
cout << ' ';
}cout << '*' << endl;
}// draw lower leg
for(nStar = 0;nStar < nWidth;nStar++)
{
cout << '*';
}
cout << endl;
// ask for another
cout << "Would you like to go again(Y/N)? ";
cin >> cChoice;
}
while(toupper(cChoice) == 'Y');return 0;
}

Wow Dave... you provided the entire solution to the homework problem, it would have at least been in her interests to figure a little bit out on her own. lol
Emily, even though his solution works, you will need to understand it as I am sure you will be tested on it and in programming everything is built on top of everything else... so you need to have an understanding of the foundation.
IR

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |