0

Ok, so I'm new to programming and I'm making console programs right now. I tried making a program to draw a rectangle in a cli program. I have some logical errors in the program that prevent it from working. I'm new to C++ so go easy on me. :P

/*Program by Ted. 11/03/11
Program creates a rectangle or square
in a text matrix according to user input.*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; //variables must be greater than 2
int w1(0);
int w2(0);
int wf(0);
int h(0);
int hf(0);
int inside(0);
int inside_c(0);
int p(0);
int a(0);
bool repeat(true);
string response("");
/*Program determines expected values. to be debugged*/
int main(){
	do{
		cout << "Enter only whole numbers.\n";
		cout << "/////////////////////////";
		cout << "\n\tEnter the width: ";
		cin >> w1, w2, wf, inside;
		inside-=2;
		inside_c = inside;
		cout << "\n\tEnter the height: ";
		cin >> h, hf;
		h-=2;
		cout << "\n" << endl;
		while(w1>0){
			cout << "/";
			w1-=1;}//end while
		cout << "\n";
		while(h>0){
			if(inside_c == 0){
				inside_c = inside;}//end if
			cout << "/";
			while(inside_c>0){
				cout << "*";
				inside_c-=1;}//end inner loop
			cout << "/\n";
			h -=1;}//end while
		while(w2>0){
			cout << "/";
			w2-=1;}//end while
		p=hf+hf+wf+wf;
		a=wf*hf;
		cout << "\n\nThe perimeter of this shape is" << p << endl;
		cout << "The area of this shape is " << a << endl;
		cout << "\nEnter dimensions for another rectangle? (lowercase only) " << endl;
		getline(cin, response);
		if (response == "y"){
			repeat = true;}
		else
			if (response == "n"){
				repeat = false;}
			else{
				cout << "Response is not understood. Program will continue. ";
				repeat = true;}//end if else
	}while(repeat == true);
	cout << "press the ENTER key to exit... ";
	cin.get();
	return 0;
}//end of main
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mlesniak
0

Beware of changing the values of data entered by the user; you may need to use them more than once in a program run.
I've modified your code a little, replacing a couple of Whiles with a for loop, nested in another for loop, to display the rectangle using the / character.
The code was attempting to assign values to several variables using one cin statement. This did not work, so only the first variable was assigned a value in each cin statement.
I would recommend that the default action would be to end the program if the user's choice of another rectangle could not be worked out. I think it's safer. There is less chance of an infinite loop occurring.

/*Program by Ted. 11/03/11
Program creates a rectangle or square
in a text matrix according to user input.*/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; //variables must be greater than 2
int w1(0);
int w2(0);
int wf(0);
int h(0);
int hf(0);
int inside(0);
int inside_c(0);
int p(0);
int a(0);
bool repeat(true);
string response("");
/*Program determines expected values. to be debugged*/
int main(){
	do{
		cout << "Enter only whole numbers.\n";
		cout << "/////////////////////////";
		cout << "\n\tEnter the width: ";
		cin >> w1, w2, wf, inside;		// w1 is assigned a value but w2, wf and inside are not.
		inside-=2;		// sets inside to -2
		inside_c = inside;
		cout << "\n\tEnter the height: ";
		cin >> h, hf;	// h is assigned a value but hf is not.
		// h-=2;			// sets h to -2
		cout << "\n" << endl;

		for (int height = 1;height <= h; height++)
		{
			for (int width = 1;width <= w1;width++)
			{
				cout << "/";
			}//end while
			cout << endl;
		}
		cout << "\n";

		/*
		while(h>0){
			if(inside_c == 0){
				inside_c = inside;}//end if
			cout << "/";
			while(inside_c>0){
				cout << "*";
				inside_c-=1;}//end inner loop
			cout << "/\n";
			h -=1;}//end while
		while(w2>0){
			cout << "/";
			w2-=1;}//end while
		*/
		
		// p=hf+hf+wf+wf;
		p = 2 * (h + w1);

		// a=wf*hf;
		a = h*w1;


		cout << "\n\nThe perimeter of this shape is " << p << endl;
		cout << "The area of this shape is " << a << endl;
		cout << "\nEnter dimensions for another rectangle? (lowercase only) " << endl;
		// getline(cin, response);
		cin >> response;
		if (response == "y"){
			repeat = true;}
		else
			if (response == "n"){
				repeat = false;}
			else{
				cout << "Response is not understood. Program will continue. ";
				repeat = true;}//end if else
	}while(repeat == true);
	cout << "press the ENTER key to exit... ";
	cin.get();
	return 0;
}//end of main
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.