| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Assignment Help to make triangle
Hello everyone
I'm trying to do my assignment. I worked on this assignment for 3 weeks now. But I couldn't figure it out. What I have to do is make a triangle, and a rectangle, and I have to output these shapes to a file called "shapes.txt" First, I have to ask the user if the user wants to output to a file, or to a monitor. The options are as follows: T: print a right triangle. This should be followed by three integers. The first number is either 1 or -1, indicating whether the triangle will have an orientation like this right side triangle, * * * * * **** or left side right triangle * * * * * **** The second number indicates the length of the sides, and the third number indicates the position of the triangle with respect to the left (i.e. the number of blanks before the triangle starts). R: print a rectangle. This should be followed by three integers, indicating the height and width of the rectangle and the position of the rectangle with respect to the left. B: print a blank line. This should be followed by one integer, indicating the number of blank lines to be printed. Q: quit the program. This is the example of the program. * ** * * * * * * <--15 spaces-->* * * * * * * * ********** So, I wrote the pseudocode like this, (my instructor helped, which I don't understand fully) Draw a triangle Ask the user three numbers, each for whether the triangle will have a left side right triangle, or a right side right triangle. The second number is for the length of the sides, and the third number is for the position of the triangle with respect to the left. 1. For first line - Print one star 2. For line 2 to n-1 line, - Draw ‘b’ blanks - Draw ‘*’ - Draw (I – 2) blanks - Draw ‘*’ 3. For the last line - Draw ‘b’ blanks - Draw ‘s’ stars Draw a rectangle Ask the user for three numbers, indicating the height and the width of the rectangle and the position of the rectangle with respect to the left. 1. For line 1 and the last line - draw ‘b’ blanks - draw ‘s’ stars 2. For line 2 to line n – 1 - draw ‘b’ blanks - draw ‘*’ - draw (s-2) blanks - draw ‘*’ This is the code I wrote. #include <iostream> #include <string> #include <fstream> using namespace std; int main() { int height = 0; int row; int col; int blanks; char ini1 = 0, ini2; cout << "Do you want to print the shapes to a file (F) or to a monitor (M)?: " << ini1 << endl; cin >> ini1; ofstream outFile; if (ini1 == 'F') { cout << "It will print out to a file\n" << endl; outFile.open("shapes.txt"); } else cout << "It will print out to a monitor\n." << endl; cout << "Please enter what kind of shape you want.\n" << "Type 'T' for a triangle.\n" << "Type 'R' for a rectangle.\n" << "Type 'B' to print a blank line.\n" << "Type 'Q' to quit this program.\n" << "If typed 'T', also type 1 for left side triangle, or -1 for right side triangle.\n" << "After that, type in length and the number of blank spaces. " << endl; cin >> ini2; //Rectangle if (ini2 == 'R') { cout << "Rectangle" << endl; row = 0; col = 0; cin >> height; while (col < height * 2) { cout << '*'; col++; } row++; while (row < height - 1) { col = 0; while (col <= height * 2) { col++; if (col == 1) cout << '*'; else { if (col == height * 2) cout << '*'; else cout << ' '; } } cout << endl; row++; outFile << ini2 << endl; } col = 0; while (col < height * 2) { cout << '*'; ++col; } cout << endl; } //Triangle if (ini2 == 'T') { cout << "Triangle" << endl; row = 0; cin >> height; while (row < height - 1) { col = 0; while (col < height + row) { ++col; if (col == height - row) cout << '*'; else { if (col == height + row) cout << '*'; else cout << ' ' ; } } cout << endl; ++row; } } col = 0; while (col < height * 2 -1) { cout << '*'; ++col; } //To print a blank line if (ini2 == 'B') { cout << endl; } //To quit a program if (ini2 == 'Q') outFile.close(); return 0; } I get the rectangle shape right, except the empty blanks on the front. How should I do this? Also, could someone help me with outputting the files too? Please help me. Thanks! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Assignment Help to make triangle |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|