Hi i am trying to draw a frame with asterisks, i dont know what is wrong with my code. I've tried to use this link http://www.cplusplus.com/src/triangle.zip for assistance but i cant open the zip file.
// Warning: I didn't try to compile this...
#include <cassert>
#include <string>
#include <iostream>
usingnamespace std;
void displayFrame( int w, int h ) {
assert( h >= 2 ); // Height has to be at least 2 for a top and bottom side
assert( w >= 2 ); // Width has to be at least 2 for a left and right side
// Draw row of 'w' asterisks for top side
cout << string( w, '*' ) << endl;
// Draw sides: asterisk, width-2 spaces, asterisk
for( int j = 0; j < h - 2; ++j )
cout << '*' << string( w - 2, ' ' ) << '*' << endl;
// Draw row of 'w' asterisks for bottom side
cout << string( w, '*' ) << endl;
}
include <string>
#include <iostream>
usingnamespace std;
void displayFrame( int w, int h ) {
assert( h >= 2 ); // Height has to be at least 2 for a top and bottom side
assert( w >= 2 ); // Width has to be at least 2 for a left and right side
// Draw row of 'w' asterisks for top side
cout << string( w, '*' ) << endl;
// Draw sides: asterisk, width-2 spaces, asterisk
for( int j = 0; j < h - 2; ++j )
cout<< '*'<< string(w - 2, ' ')<< '*' << endl;
// Draw row of 'w' asterisks for bottom side
cout << string( w, '*' ) << endl;
}
int main()
{
int width, height;
cout << "What width (greater than 4) frame would you like to draw?";
cin >> width;
cout << "What height (greater than 4) frame would you like to draw? ";
cin >> height;
cout << endl;
displayFrame(width, height);
cout << endl << endl;
return 0;
}
I'm not sure what else to say. The new code you posted is almost identical to your original post and still has the problems in it that I identified in my original response. I also gave you a complete solution to the problem which you should be able to trivially modify to meet your new requirement.
If you can post some new code that shows you've made some progress I can help further.
jsmith thank you again.
The above program works 100% correct. I've tried to come up with another one. It is a regtangle and double sided with #, the one i did is not equall on both sides. Please help
#include <iostream>
usingnamespace std;
void displayFrame( int w, int h ) {
assert( h >= 2 ); // Height has to be at least 2 for a top and bottom side
assert( w >= 4 ); // Width has to be at least 4 for a left and right side
// Draw row of 'w' hash for top side
cout << string( w, '#' ) << endl;
cout << string( w, '#' ) << endl;
// Draw sides: hash, width-2-2 spaces, asterisk
for( int j = 0; j < h - 2; ++j )
cout << '#'<< '#' << string(w - 2-2, ' ')<< '#' << '#' << endl;
// Draw row of 'w' hash for bottom side
cout << string( w, '#' ) << endl;
cout << string( w, '#' ) << endl;
}
int main()
{
int width, height;
cout << "What width (greater than 4) frame would you like to draw?";
cin >> width;
cout << "What height (greater than 4) frame would you like to draw? ";
cin >> height;
cout << endl;
displayFrame(width, height);
cout << endl << endl;
return 0;
}
Let's look at your width. When you ask the user what width they want, the rectangle you draw includes the sides in the width (which makes sense, because the top and bottom sides are included in the height).
So having said that, you have to divide the width into three pieces:
* The first two characters of width are for the '##' on the left side.
* The last two characters of width are for the '##' on the right side.
* That leaves width - 2 - 2 spaces in between.
Given that, there is a very simple fix (change 1 line) to the above program.
And I'll give you a hint. You also need to change line 8 to
assert( w >= 4 ); // Width has to be at least 4 for a left and right side
Sure, but circles are more difficult to draw in text mode and off the top of my head I don't recall the formulas needed (requires sin and cos) to compute the (X,Y) coordinates.