Computing.Net > Forums > Programming > simple C++ code help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

simple C++ code help

Reply to Message Icon

Name: emily
Date: October 13, 2003 at 12:35:26 Pacific
OS: Windows XP
CPU/Ram: Pentium2
Comment:

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 :)



Sponsored Link
Ads by Google

Response Number 1
Name: JasonR
Date: October 13, 2003 at 13:04:23 Pacific
Reply:

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;
}


0

Response Number 2
Name: emily
Date: October 13, 2003 at 13:42:11 Pacific
Reply:

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 :)


0

Response Number 3
Name: schooner22
Date: October 13, 2003 at 15:22:46 Pacific
Reply:

Try using for loops and if statements.


0

Response Number 4
Name: Emily
Date: October 13, 2003 at 15:29:16 Pacific
Reply:

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.


0

Response Number 5
Name: Infinite Recursion
Date: October 14, 2003 at 08:04:45 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Infinite Recursion
Date: October 14, 2003 at 08:06:05 Pacific
Reply:

Oh... a hint: Test the program with rows = 4 and columns = 3.

IR


0

Response Number 7
Name: Dave
Date: October 16, 2003 at 09:35:03 Pacific
Reply:

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;
}


0

Response Number 8
Name: Infinite Recursion
Date: October 17, 2003 at 06:30:47 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: simple C++ code help

Help in C coding www.computing.net/answers/programming/help-in-c-coding/4606.html

Programming a proxy in C code www.computing.net/answers/programming/programming-a-proxy-in-c-code/6551.html

Simple C++ prog help plz www.computing.net/answers/programming/simple-c-prog-help-plz/12574.html