c++ - Draw A Rectangle With Asterisks -
i trying write c++ program display rectangle drawn in asterisks. have program running except fact 1 side of heights of rectangles print. here code have written display rectangle method.
void rectangle::displayrectangle() { int i=0, j=0; (int = 0; < width; i++) { cout << "*"; } cout << endl; (int = 0; < height - 2; i++) { cout << "*"; (int j = 0; j < width; j++) { cout << " "; } cout << endl; } (int = 0; < width; i++) { cout << "*"; } cout << endl; }
specify width , height @ start need 3 loops. first print top line of rectangle. second print both sides of rectangle (minus top , bottom of sides). third print bottom line of rectangle.
like so
// width , height must both @ least 2 unsigned int width = 7; // example value unsigned int height = 5; // example value // print top row for(unsigned int = 0; < width; i++); { std::cout << "*"; } std::cout << std::endl; // print sides for(unsigned int = 0; < height - 2; i++) { std::cout << std::setw(width - 1) << std::left << "*"; std::cout << "*" << std::endl; } // print bottom row for(unsigned int = 0; < width; i++) { std::cout << "*"; } std::endl; you need include both iostream , iomanip work (setw part of iomanip).
the top , bottom rows done using method fill spaces given character, cannot recall method right now.
Comments
Post a Comment