Hi I am an extreme beginner and I was given an assignment to create a program that constructs shapes (rectangle and triangle) which I completed. Now I need to do the same thing except using functions and I am having quite a bit of trouble. This is the part of the assignment I am struggling on:
Re-structure your program with the following functions:
int Rectangles ( int w, int h, char fchar, int & numchars). The parameters are the width, height, character to use, and the number of characters output. If the width is greater than 20 or less than 1 and/or the height is greater than 20 or less than 1, the rectangle() will return –1 via the return statement. If the figure is created, the return value is 0. The calling program should check the return to decide whether the figure was printed. If not, print the error message and ask for another input. If printed, then print the number of characters in the figure.
int Triangles( int h, char fchar, int& numchars). Same requirements as Rectangles except there is no width parameter.
Use at least one function within the figure functions. The output for one line should created by a function which has parameters of the number of characters to output in a line and the character to output.
i.e. void line ( int numchars, char pchar ).
And this is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int Rectangles(int w, int h, char fchar)
{
int line, loop;
for (line = 1; line <=h; line++)
{
for (loop = 1; loop <= w; loop++)
cout << fchar;
cout << endl;
}
}
int main()
{
//Declare variables x y z and char s
int x, y, z;
char s;
//Gather input for shape and type of character
do {
cout << "Enter 1 for a rectangle or 2 for a triangle. Enter a negative number to exit. " << endl;
cin >> x;
//Check validity of input for shape
if(x == 1 || x == 2)
{
cout << "Enter a fill character you would you like to use " << endl;
cin >> s;
//Exit program if first input is negative
if(x < 0)
break;
}
//Gather final input for height and if needed width
//Check validity of final input
//Output error message for height and width greater than 20 or less than 1
if(x==1)
{
cout << "Enter the height of your rectangle" << endl;
cin >> y;
while(y > 20 || y < 1)
{
cout << "Error! Your height is invalid, please input a value between 1 and 20."<< endl;
cin >> y;
}
cout << "Enter the width of your rectangle" << endl;
cin >> z;
while(z > 20 || z < 1)
{
cout << "Error! Your width is invalid, please input a value between 1 and 20."<< endl;
cin >> z;
}
}
//Write out constraints
//Use loops to print character selected by user
int Rectangles(z, y, s);
if(x==2)
{
cout << "Enter the height of your triangle" << endl;
cin >> y;
while(y > 20 || y < 1)
{
cout << "Error! Your height is invalid, please input a value between 1 and 20."<< endl;
cin >> y;
}
int line2, fill2, loop2;
for (line2 = 1; line2 <=y; line2++)
{
fill2 = line2;
for (loop2 = 1; loop2 <= fill2; loop2++)
cout << s;
cout << endl;
}
}
//Loop program back to input stage
}
while (x >= 0);
return 0;
}
|
I am just really unsure of how to go about this. Any help would be appreciated. Thank you.