Hey everyone I am kind of stuck with these function. i am trying to work ahead to get stuff done faster and this is what my output is so far. I am stuck on the width part. I am not trying to have anyone do my assignment for me and the reason I put all the output is for future reference and to have everyone get a feel of my project. At the bottom I will display my instructions and my entire output should be:


Programmed by <your name>

Type a negative for the height to exit!
Enter the height: 7
Enter the width: 5
Enter the character to fill the rectangle with: +
- - - - -
:+++:
:+++: (By the way i am trying to make this figure even out in spacing)
:+++:
:+++:
:+++:
_ _ _ _ _

Enter the height at the first prompt and the width at the second!
Type a -1 for the first prompt to exit!
Enter the height: 1
Invalid: Height cannot be less than 3.
Type a negative height to exit! Try again!!!
Enter the height: 0
Invalid: Height cannot be less than 3.
Type a negative height to exit! Try again!!!
Enter the height: 5
Enter the width: 1
Invalid: Width cannot be less than 3.
Try Again!!!
Enter the width: -4
Invalid: Width cannot be less than 3.
Try Again!!!
Enter the width: 2
Invalid: Width cannot be less than 3.
Try Again!!!
Enter the width: 10
Enter the character to fill the rectangle with: @

_ _ _ _ _ _ _ _ _ _
:@@@@@@@@:
:@@@@@@@@:
:@@@@@@@@:
_ _ _ _ _ _ _ _ _ _

Enter the height at the first prompt and the width at the second!
Type a -1 for the first prompt to exit!
Enter the height: 1
Invalid: Height cannot be less than 3.
Type a negative height to exit! Try again!!!
Enter the height: -4

#include <iostream>
using namespace std;
double Get_Character();
double Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{
	cout << "Programmed by Bryan Kruep";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";

	double cha, height = 0, width = 0;
	cha = Get_Character();
	while (cha != -1)
		{
		
		
		cha = Get_Character();
	}
	return 0;
}

	
	double Get_Character()
{
    double height;
	

	cout << "\nEnter the height: ";
	cin >> height;

	cout << endl << "Enter the width: ";
	cin >> width;

	return (height);
}

Write a program that asks the user to type in the height and width of a rectangle. It also asks the user to type in a character in which to fill the rectangle. The program prints the top and bottom of the rectangle using a hyphen ("-") and the sides using a pipe ("|"). The interior of the rectangle will be filled with the character the user types. Because of the fill character, the height and width of the rectangle cannot be less than 3, because if it is less than three, there would be no place to fill. If the user types an invalid height or width, the program should tell them it is invalid and ask them to type it in again. The program should keep accepting heights and widths until the user types a negative number for the height.

You should use the following functions:
Draw_Rectangle – The height, width and character are passed into this function. It calls the functions Draw_Top_Bottom and Draw_Middle. Nothing is returned from this function.
Draw_Top_Bottom – This function accepts an integer as the formal parameter and draws the top (or bottom) of the rectangle. Nothing is returned from this function.
Draw_Middle – This function accepts the height, width and the filler character as the parameters and prints the rows of the rectangle. Nothing is returned from this function.
Get_Number – This function has a prompt (a string) passed into the function and asks the user to type what the prompt asks. The function returns the number that is typed in. It does not check for the negative number (that is done in main)
Get_Character – This function asks the user to type in the filler character and returns it.

Recommended Answers

All 57 Replies

//basic main function
int main()
  int height, width
  height = Get_Number(1);
  width = Get_Number(2);

//Get_Number prototype
int Get_Number(int x)
   sentinnel variable
   input variable
   
  use loop until user get's it right
      ask user for height if x == 1
      else ask user for width

      accept input
      if input less than 0 
          do something
      else if input >= 0 and less than 3
          do something 
      else //input valid
         do something 
  return input

so what i have done so far is wrong???

From how I read the instructions, yes and no. You need a Get_Character function, but it returns a char, not an numerical value. Height and width have nothing to do with the Get_Character function. They should be obtained using the Get_Number function. cha should be type char, height and width should be type int. Line 15 should be in Get_Number function, not in main().

Still having errors if anyone has any suggestions plese take a look if very familiar w\functions

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{
	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";

	int height, width;
	char x;
	height = Get_Number();
	width = Get_Number();

	
	cin >> height;
	cin >> x;
	while (x = 1)
		{
		
		
		height = Get_Number();
	}
		width = Get_Number();
	return 0;
}

	
	int Get_Number()
{
    int height;
	int width;
	

	cout << "\nEnter the height: ";
	cin >> height;

	cout << endl << "Enter the width: ";
	cin >> width;

	return (height);
}
cin >> height;
	cin >> x;
	while (x = 1)
		{
		
		
		height = Get_Number();
	}

You have an infinite loop here. Once you enter the while loop, you will never leave it. One, I believe you are mixing up the assignment (=) operator and the equality operator (==). Two, your condition refers to the variable x, but inside your while loop the variable x is never changed. The variable height is. Also please refer to my comments in your other thread regarding Get_Number. You ask the user for input for the variable width, but that input is lost when the function ends and is thus never used.

well now it looks like you don't have anything to exit your loop and it keeps repeating...

change your while loop to something like

while (x>=-1)

I don't quite understand why you are asking for the height and width twice...

Ok I just want everyone to know we really havent gone over functions in my class yet much I am trying to just get this program done before spring break so when you talk about function definitions and stuff im lost on what you mean. I am not asking for answers but can someone look through what I have so far and just highlight what is wrong...I don't even care if you tell me what to change it to or not I am just been lost all night long and don't know what to change

]

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";

	int height, width;
	char x;
	
	height = Get_Number();
	width = Get_Number();
	

	while (x = -1)
		
	height = Get_Number(1);
	
	width = Get_Number(2);
	
	return 0;
}

	int Get_Number()
{
    int height;
	int width;
	

	cout << "\nEnter the height: ";
	cin >> height;

	cout << "Enter the width: ";
	cin >> width;

	return (height);
}

Ok I just want everyone to know we really havent gone over functions in my class yet much I am trying to just get this program done before spring break so when you talk about function definitions and stuff im lost on what you mean. I am not asking for answers but can someone look through what I have so far and just highlight what is wrong...I don't even care if you tell me what to change it to or not I am just been lost all night long and don't know what to change

]

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";

	int height, width;
	char x;
	
	height = Get_Number();
	width = Get_Number();
	

	while (x = -1)
		
	height = Get_Number(1);
	
	width = Get_Number(2);
	
	return 0;
}

	int Get_Number()
{
    int height;
	int width;
	

	cout << "\nEnter the height: ";
	cin >> height;

	cout << "Enter the width: ";
	cin >> width;

	return (height);
}

Line 26 - still have the problem between = and ==. It needs to be ==. You have declared x to be a character. A character cannot equal -1. I don't understand the purpose of x in this while loop anyway. Seems to me that you should be comparing whether height is less than 0 in that while loop, not x.

this while loop is never ending

while (x = -1)

the way you have it set up is that x is always equal to one so it keeps outputting for you to input the height and width then never stores it because it repeats again. Also x should be an int not a char.

if you make the while loop to equal

while (x>=-1)

it will make it stop the loop and store the inputed height and width after you input a negative number.

your function

double Get_Character();

outside of the main is not doing anything. You are calling it into the main but then it does not do anything. They way your program is set up it would just be easier to not use a function for you since you don't know how to use them yet.

I know you guys think I am a complete idiot by now but I think I did what you told me to do and still having problems so I am guessing my braces are wrong.....

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";

	int height, width, x;
	
	
	height = Get_Number();
	width = Get_Number();
	

	while (x >= -1)
		
	{x = Get_Number();
	
	}
	
	return 0;
}

	int Get_Number()
{
    int height;
	
	cout << "\nEnter the height: ";
	cin >> height;

	return (height);
}

1) move line 19 to line 16
2) then move line 17 to line 21
3) on line 20 add this line: cout << "input the height of a box" << '\n';
4) remove lines 26 through 30 and lines 39 and 40.
5) change the name of the variable called height on lines 37 and 42 to x or temp or whatever, but not height, as the Get_Number function will be used to obtain user input for both height and width.

On line 38 declare a bool variable called inputInvalid and intialize it to true.
On line 39 use a while loop with inputIvalid within the parenthesis.
Within the body of the loop:
1) obtain input from the user placing it in x or temp or whatever.
2) evaluate the input using if/else statements:
a) if input is less than zero then the user decided to exit the program so exit the program.
b) else if input is 0, 1, or 2 then can't make a box since, by instruction, the box must be minimum height of 3 and minimum width of 3. So you need to obtain new input. It's user friendly to tell the user that a data entry error has occurred and to provide them with explicit instructions each time you need/want input from them, so do it now, again.
c) else user input is valid so change the value of invalidInput to false to stop the loop.

Still getting a ton of errors:

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;
	
	int height, width, x;
	cout << "input the height of a box" << '\n';
	
	cout << "Type a negative for the height to exit!";
	height = Get_Number();
	width = Get_Number();
	
	return 0;
}

	int Get_Number()
{
    int x;
	bool inputInvalid = true;

	{while(inputInvalid == x)
	
		if (x <= -1);
		{
		}
		else if (x = 1 || x = 2 || x = 3)
		{
			cout << "Invalid! Height cannot be less than 3.";
		}

		else
			inputInvalid = false;
	

	}
	return (x);
}

Still getting a ton of errors:

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;
	
	int height, width, x;
	cout << "input the height of a box" << '\n';
	
	cout << "Type a negative for the height to exit!";
	height = Get_Number();
	width = Get_Number();
	
	return 0;
}

	int Get_Number()
{
    int x;
	bool inputInvalid = true;

	{while(inputInvalid == x)
	
		if (x <= -1);
		{
		}
		else if (x = 1 || x = 2 || x = 3)
		{
			cout << "Invalid! Height cannot be less than 3.";
		}

		else
			inputInvalid = false;
	

	}
	return (x);
}

You have no "cin" statement in your Get_Number function anymore. What happened to it? You need one in there. In line 31, your "while" statement should be BEFORE the bracket, not after. And you don't want the "== x" part. inputInvalid is a boolean variable. The while loop should be like this:

while (inputInvalid)
{
    // code
}

You need a cin statement. x is uninitialized. If x is the user input, you need the line:

cin >> x;

before you make any comparisons to x. In line 36:

else if (x = 1 || x = 2 || x = 3)

you are confusing the = operator and the == operator again. Those need to be == since you are comparing x to values.

ok I am starting this piece of **** over can someone just help me get started with the loop....this is what I got so far and can;t remember how to get this to loop...

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number(string prompt);
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

    int height, width;

    cout << "Programmed by Jim johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    do
    {

    cout << "Enter the height: ";
    cin >> height;

    cout << endl;

    } while (height >= 0);


        cout << "Enter the width: ";
        cin >> width;

        cout << endl;



return (0);
}

sorry heres with tags

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number(string prompt);
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{

    int height, width;

    cout << "Programmed by Jim Johnson";
    cout << endl << endl;

    cout << "Type a negative for the height to exit!";
    cout << endl;

    do
    {

    cout << "Enter the height: ";
    cin >> height;

    cout << endl;

    } while (height >= 0);


        cout << "Enter the width: ";
        cin >> width;

        cout << endl;



return (0);
}

or is restarting like this really going to mess me up?

#include <iostream>
using namespace std;

int Get_Number();

int main()
{ 	
   int height, width;	

   cout << "input the height of a box" << '\n';     
   height = Get_Number();

   cout << "input the width of a box" << '\n';
   width = Get_Number(); 	
  
   return 0;
} 	

int Get_Number()
{    
    int x;	
    bool inputInvalid = true; 
	
    while(inputInvalid)
    {         
        cout << "Type a negative integer to exit the program!" << '\n';
        cin >> x;

        if (x < 0)		
        {	
           cout << "Bye";
           exit(1);	
        }		
        else if (x == 0  || x == 1 || x == 2)	
        {			
             cout << "Invalid! Input cannot be less than 3." << '\n';	
         } 	
         else	
         {	
             inputInvalid = false; 
         } 	
   }	
  return x;
}

I don't want you guys to take this the wrong way but I just copied exactly what you put into a separate program and ran it and it didn't work...if anyone is not sure how to do this I need someone that does

#include <iostream>
using namespace std;
double Get_Character();
int Get_Number();
void Draw_Rectangle();
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{
	
	int height, width;

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;


	
	cout << "Enter the height: ";
	height = Get_Number();

	cout << "Enter the width: ";
	width = Get_Number();

	return 0;
}

int Get_Number()
{
	int x;
	bool inputInvalid = true;
	while (inputInvalid)
	{         
        cout << "Type a negative for the height to exit!" << '\n';
        cin >> x;
		
		if {x < 0);
		{
			exit(1);
		}
		else if (x == 0  || x == 1 || x == 2)
		{
			cout << "Invalid!! Height can not be less than 3. << Type a negative height to exit! Try Again !!!";
		}
		else
		{
		inputInvalid = false;
	} 
}
	return x;
}

I don't want you guys to take this the wrong way but I just copied exactly what you put into a separate program and ran it and it didn't work...if anyone is not sure how to do this I need someone that does

This line didn't copy correctly:

if {x < 0);

You have a bracket instead of an opening parentheses and you have a semicolon at the end. Take the semicolon out and change the opening bracket to an opening parentheses and the program works. It could use a slightly better prompt in Get_Number since you are getting the width and the height there. I hope you didn't type it in word for word. Simply hit the "Toggle Plain Text" to get rid of the line numbers (if any), highlight the code, "copy", then "paste" to your document. The above mistake won't happen and the formatting will be preserved.

It worked fine for me. If you want to keep asking the questions till the user enters a negative number, just put the code in an infinite loop in the main function like this:

while (true)
{
	cout << "Enter the height: ";
	height = Get_Number();

	cout << "Enter the width: ";
	width = Get_Number();
}

I don't want anyone telling me how to make this rectangle work but does anyone know any sites where I can find any information on making rectangles from my program because I have nothing in my textbook

The program you are to write isn't likely to be found on any site or in any book other than your instructor's website/instruction manual, if they have one. The instructions you are provided seem quite straight forward. Stick to writing just one function at time, testing it as you go, and it should work out fine.

The code I posted doesn't do it quite the way the instructions indicate, by the way. The instructions indicate you are to pass a string to Get_Number(), the contents of the string being the information to prompt the user what to enter. I think you can figure out how to do that. If not post your effort for further critique.

In addition, the program should allow for more than one box to be drawn during the running of the program. You should use a loop like Vernon Dozier demostrated to do that.

To complete the task once you have a completed the Get_Number() function (and you can get as many values for height and width as the user wants to enter), you can use a very similar technique to get the fill character. It's actually easier, since the input doesn't have to validated.

Finally, the function bodies of the display functions are basically just loops whose instructions vary a little bit based on whether it's being used to draw the top/bottom or one of the middle lines making up the box.

verizon which loop was he referring to?

The program should keep accepting heights and widths until the user types a negative number for the height.

The above statement taken from the instructions section of post #1 of this thread probably means that the program should allow the user to draw more than one box if they want when they run the program. That means that you have to be able to get the information for height, width and fill char in addition to doing the drawing as many times as the user wants. That means you'll have to use a loop very similar to what VernonDozier described in post #20 of this thread, though you'll want the loop to include all of the appropriate function calls needed to create the box, (when they are written), not just the function calls to get the height and width.

i really dont understand how the post #20 is going to affect my rectangle i need to get

i really dont understand how the post #20 is going to affect my rectangle i need to get

The loop I posted in post 20 has nothing to do with the drawing of the rectangle. It has to do with repeating a block of code over and over again. You want the program to continually execute a task (in your case, prompting for data and then drawing the rectangle). You need to develop the code to draw a single rectangle from the user inputted dimensions. You'll put that rectangle drawing code inside the loop I posted (or perhaps you'll put a CALL to a FUNCTION that draws the rectangle inside the loop I posted). This will repeatedly ask for the dimensions and draw the rectangle. The repetition of a block of code is the aspect of your program for which Lerner is referring you to my loop, not the actual task of drawing the rectangle itself.

Note that there are other loop designs you can use. I designed my loop to correspond with Lerner's function. That function handles the program exit aspect, so the loop I designed did not have to.

am I at least getting it or just completely off:

#include <iostream>
using namespace std;
char Get_Character();
int Get_Number();
void Draw_Rectangle(int height, int width);
void Draw_Top_Bottom();
void Draw_Middle();

int main()
{
	
	int height, width;
	char symbol;

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";
	cout << endl;

	cout << "Enter the height: ";
	height = Get_Number();

	cout << "Enter the width: ";
	width = Get_Number();

	cout << "Enter the character to fill the rectangle with: ";
	cin >> symbol;

	Draw_Rectangle(height, width);

	



	
	return 0;
}

void Draw_Rectangle(int height, int width)
{
	int a = height;
	int b = width;
}

void Draw_Top_Bottom()
{
	int width, count = 0;
	char top, bottom, _;
	

	top = _ * width;
	cin >> top;

}

int Get_Number()
{
	int x;
	bool inputInvalid = true;
	while (inputInvalid)
	{         
        
        cin >> x;
		
		if (x < 0)
		{
			exit(1);
		}
		else if (x == 0  || x == 1 || x == 2)
		{
			cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
		}
		else
		{
		inputInvalid = false;
	} 
}
	return x;
}

you need a loop in main() so user can draw multiple rectangles durning any given run of the program, but that can wait until you can draw at least one rectangle if you wish.

There are quite good instructions in terms of requirements in post #1. You really should look at them some more. For example, it says that user should input the fill character within a function called Get_Character(), or some name like that.

The posted instructions also say that Draw_Rectangle should be passed three parameters not 2 and that it calls Draw_Top_Bottom and Draw_Middle, or whatever the names are.

Furthermore, Draw_Top_Bottom gets one parameter, not none.

There are a bunch of other details there as well.

Then, in Draw_Top_Bottom you use cin >> which is an input function. You want an output function. You also want a loop so you can output at least three, and maybe more underscore characters with each call to Draw_Top_Bottom.

does this look any better i am sorry i really dont understand alot of the terminology from some of these posts...

#include <iostream>
using namespace std;
char Get_Character();
int Get_Number();
void Draw_Rectangle(int height, int width, char symbol);
void Draw_Top_Bottom(int width);
void Draw_Middle();

int main()
{
	
	int height, width;
	char symbol;
	

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";
	cout << endl;

	cout << "Enter the height: ";
	height = Get_Number();

	cout << "Enter the width: ";
	width = Get_Number();

	Draw_Top_Bottom(char );

	Draw_Middle();

	cout << "Enter the character to fill the rectangle with: ";
	symbol = Get_Character();
	
	return 0;
}

void Draw_Top_Bottom(int width)
{


}

void Draw_Middle(char symbol)
{



}

void Draw_Rectangle(int height, int width, char symbol)
{



}

int Get_Number()
{
	int x;
	bool inputInvalid = true;
	while (inputInvalid)
	{         
        
        cin >> x;
		
		if (x < 0)
		{
			exit(1);
		}
		else if (x == 0  || x == 1 || x == 2)
		{
			cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
		}
		else
		{
		inputInvalid = false;
	} 
}
	return x;
}

does this look any better i am sorry i really dont understand alot of the terminology from some of these posts...

#include <iostream>
using namespace std;
char Get_Character();
int Get_Number();
void Draw_Rectangle(int height, int width, char symbol);
void Draw_Top_Bottom(int width);
void Draw_Middle();

int main()
{
	
	int height, width;
	char symbol;
	

	cout << "Programmed by Jim Johnson";
	cout << endl << endl;

	cout << "Type a negative for the height to exit!";
	cout << endl;

	cout << "Enter the height: ";
	height = Get_Number();

	cout << "Enter the width: ";
	width = Get_Number();

	Draw_Top_Bottom(char );

	Draw_Middle();

	cout << "Enter the character to fill the rectangle with: ";
	symbol = Get_Character();
	
	return 0;
}

void Draw_Top_Bottom(int width)
{


}

void Draw_Middle(char symbol)
{



}

void Draw_Rectangle(int height, int width, char symbol)
{



}

int Get_Number()
{
	int x;
	bool inputInvalid = true;
	while (inputInvalid)
	{         
        
        cin >> x;
		
		if (x < 0)
		{
			exit(1);
		}
		else if (x == 0  || x == 1 || x == 2)
		{
			cout << "Invalid!! Height can not be less than 3." << endl << "Type a negative height to exit! Try Again !!!" << endl << "Enter the height: ";
		}
		else
		{
		inputInvalid = false;
	} 
}
	return x;
}

Draw_Top_Bottom and Draw_Middle are helper functions for Draw_Rectangle. You want to call both of them from the Draw_Rectangle function. You'll call the Draw_Rectangle function from main. You are going to need to pass the same parameters to Draw_Middle as you passed to Draw_Rectangle.

i am sorry i really dont understand alot of the terminology from some of these posts...

We can't explain it if you don't tell us the terminology and the post number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.