I am almost finished reading one of my tutorial books on C++. I made this program a little while back. The comments explain what the program does. There are still a few errors, like if you enter a character instead of a integer you'll be stuck in an endless loop.

Below is the code, tell me what you think and how to improve :)

/*Program by Ted. 11/03/11
Program creates a rectangle or square
in a text matrix according to user input.
Rectangle includes a border and fill character option*/
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
/*Program determines expected values. to be debugged (more).*/

void c_char(char &, char &);

int main(){
	//variable declarations
	double w[3] = {0.0, 0.0, 0.0};
	double h[2] = {0, 0};
	double panda[2] = {0, 0};
	double inside[2] = {0, 0};
	bool choices[2] = {true, false};
	string response[2] = {"", ""};
	char numbers[2] = {'/', '+'};
	///////////////////////
	cout << "This program creates a rectangle in a text matrix.\n";
	cout << "By Ted.\n";
	cout << "Program only accepts whole numbers.\n";
	cout << "/////////////////////////" << endl;
	cout << "Press 'y' for border/fill options.\nPress any other key to continue: ";
	getline(cin, response[1]);
	//could've used /variable = toupper(variable)/ for char datatype,
	//but I'm also trying to learn more on string manipulation, so... yeah.
	transform(response[1].begin(), response[1].end(), response[1].begin(), tolower);
	if(response[1] == "y"){
		c_char(numbers[0], numbers[1]);}
	do{
		do{
			//Put user values in correct arrays.
			cout << "\n\tEnter the width: ";
			cin >> w[0];
			w[0] = static_cast<int>(w[0]);
			w[1] = w[0];
			w[2] = w[0];
			inside[0] = w[0];
			inside[1] = w[0];
			inside[0]-=2;
			inside[1] = inside[0];
			cout << "\n\tEnter the height: ";
			cin >> h[0];
			h[0] = static_cast<int>(h[0]);
			h[1] = h[0];
			h[0]-=2;
			cout << "\n" << endl;
			///////////////////////////////////

			//if user enter wrong data...
			if(w[0]<1 || h[0]<1){
			w[0] = 0;
			inside[0] = w[0];
			h[0] = w[0];
			h[1] = h[0];
			cout << "Please enter a number greater than 2. \n" << endl;
			choices[1] = true;}
			else{
				choices[1] = false;}
			}while(choices[1] == true);

		//Create the rectangle
		while(w[0]>0){
			cout << numbers[0];
			w[0]-=1;}//end while
		cout << "\n";
		while(h[0]>0){
			if(inside[1] == 0){
				inside[1] = inside[0];}//end if
			cout << numbers[0];
			while(inside[1]>0){
				cout << numbers[1];
				inside[1]-=1;}//end inner loop
			cout << numbers[0] << "\n";
			h[0] -=1;}//end while
		while(w[1]>0){
			cout << numbers[0];
			w[1]-=1;}//end while
		//////////////////////

		//Calculate area and perimeter
		panda[0]=h[1]+h[1]+w[2]+w[2];
		panda[1]=w[2]*h[1];
		//////////////////////////////

		//Display area and perimeter. ("panda" stands for Perimeter AND Area. PandA)
		//((or it could mean the fluffy bear, whichever you like))
		cout << "\n\nThe perimeter of this shape is " << panda[0] << endl;
		cout << "The area of this shape is " << panda[1] << endl;
		cin.ignore(100, '\n');
		////////////////////////////////////////////////////////////////////////////

		//Ask to repeat
		cout << "\nEnter dimensions for another rectangle? (y/n) " << endl;
		getline(cin, response[0]);
		transform(response[0].begin(), response[0].end(), response[0].begin(), tolower);
		if (response[0] == "y"){
			choices[0] = true;}
		else
			if (response[0] == "n"){
				choices[0] = false;}
			else{
				cout << "Response is not understood. Program will continue. ";
				choices[0] = true;}//end if else
	}while(choices[0] == true);
	cout << "press the ENTER key to exit... ";
	cin.get();
		///////////////
	return 0;
}//end main function

void c_char(char &num1, char &num2){
	cout << "\nChoose a character for the border. ";
	cin >> num1;
	cin.ignore(100, '\n');
	cout << "\nChoose a character for the fill. ";
	cin >> num2;
	return;}

nice work but you can do better
1-try to add more functions (donot put all the code in main)
2-it would be nice if you clear the screen every itr.
3-to avoid misstype input crash try to get width as string then convert to int

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.