hi 2 all, I have an assignment about classes, everything IS OK , except of the draw function of a square.(after running the prog. there is no square drawn )
this wt I did till now:

class Square{
private:
	int sidelength;
	char ch;
public:
	Square();//default constructor
	void setSidelength(int);//sets default values
	double getSidelength(int);//gets the values 
	double getArea();//calculates the area of the square
	double getPerimeter();//calculates the perimeter of the square
	void draw(int,char);
    
};//end square.h

#include<iostream>
#include"Square.h"

using namespace std;
int L;
char ch;
int s=4;
int line, loop;
Square::Square()//default constructor
{
sidelength=1;	
}

void Square::setSidelength(int L)//sets default values to side
{
	if (L>0 && L<20)
    sidelength=L;
}

double Square::getSidelength(int L)//gets the value for 
{
	return sidelength;
}

double Square::getArea()//calculates area of square
{
	return sidelength*sidelength;
}

double Square::getPerimeter()//calculates the circumference of the square
{
	return (4*sidelength);
}

void Square::draw (int sidelength,char ch)
{
for (line=1;line<=s;line++)
{
	for (loop=1;loop<=s;loop++)
		cout<<" ";
	cin>>ch;
	cout<<endl;
}

}

	void main()
{
	int n;
char ch;
	int sidelength;
	
	Square Sqr;


cout<<"Two Dimensional Shapes System:\n";
cout<<"[1] Square\n";
cout<<"[2] Rectangle\n";
cout<<"[3] Triangle\n";
cout<<"[4] Circle\n";
cout<<"[5] Exit\n";
cout<<"Enter your choice from the menu list: ";
cin>>n;
if(n==1){
cout<<"Enter the square sidelength:";
cin>>sidelength;
Sqr.setSidelength(sidelength);

cout<<"The Perimeter = "<<Sqr.getPerimeter()<<endl;
cout<<"The area ="<<Sqr.getArea()<<endl; 
cout<<"Enter the character symbol for drawing: ";
cin>>ch;
cout<<"The rectangle is shown as:"<<endl;
Sqr.draw(sidelength,ch);

}
	}

please try to help as possible :)

Edited 6 Years Ago by Nick Evan: Added code-tags

Comments
Another god-awful post without code tags from someone running round screaming urgent - sheesh

please put your code by (code) icon to be recognized right

Edited 3 Years Ago by mike_2000_17: Fixed formatting

ok sure, here it is.

class Square{
private:
	int sidelength;
	char ch;
public:
	Square();//default constructor
	void setSidelength(int);//sets default values
	double getSidelength(int);//gets the values 
	double getArea();//calculates the area of the square
	double getPerimeter();//calculates the perimeter of the square
	void draw(int,char);
    
};//end square.h

#include<iostream>
#include"Square.h"

using namespace std;
int L;
char ch;
int s=4;
int line, loop;
Square::Square()//default constructor
{
sidelength=1;	
}

void Square::setSidelength(int L)//sets default values to side
{
	if (L>0 && L<20)
    sidelength=L;
}

double Square::getSidelength(int L)//gets the value for 
{
	return sidelength;
}

double Square::getArea()//calculates area of square
{
	return sidelength*sidelength;
}

double Square::getPerimeter()//calculates the circumference of the square
{
	return (4*sidelength);
}

void Square::draw (int sidelength,char ch)
{
for (line=1;line<=s;line++)
{
	for (loop=1;loop<=s;loop++)
		cout<<" ";
	cin>>ch;
	cout<<endl;
}

}


	void main()
{
	int n;
char ch;
	int sidelength;
	
	Square Sqr;


cout<<"Two Dimensional Shapes System:\n";
cout<<"[1] Square\n";
cout<<"[2] Rectangle\n";
cout<<"[3] Triangle\n";
cout<<"[4] Circle\n";
cout<<"[5] Exit\n";
cout<<"Enter your choice from the menu list: ";
cin>>n;
if(n==1){
cout<<"Enter the square sidelength:";
cin>>sidelength;
Sqr.setSidelength(sidelength);

cout<<"The Perimeter = "<<Sqr.getPerimeter()<<endl;
cout<<"The area ="<<Sqr.getArea()<<endl; 
cout<<"Enter the character symbol for drawing: ";
cin>>ch;
cout<<"The rectangle is shown as:"<<endl;
Sqr.draw(sidelength,ch);

}
	}

So what is wrong with the draw function?

Observation: delete those global variables, especially the ones that are used as loop counters. They should be declared inside the functions in which they are used.

The problem is after running the program, I FOUND that the square didn't appear at all, so I THINK there is something wrong, but I dun no wt?? :(
and thanx 4 the observation :)

for (line=1;line<=s;line++)
{
	for (loop=1;loop<=s;loop++)
		cout<<" ";
	cin>>ch;
	cout<<endl;
}

The problem is cin. you should have used cout, not cin.

Note: that function is far from complete. Do it something like this:
1) draw the top line
2) draw the left and right sides
3) draw the bottom line

Edited 6 Years Ago by Ancient Dragon: n/a

I tried wt u said, but only a line (vertically) was drawn :S
ANYWAY thanx alot 4 ur help.

I tried wt u said, but only a line (vertically) was drawn :S
ANYWAY thanx alot 4 ur help.

Did you forget to print the '\n' at the end of each line?

Also change this :

double getSidelength(int);//gets the values

to this :

int getSideLength(); //gets the values

There is no need for the int parameter, as well as the double return value, since you are returning your int member
variable.

This is completely wrong :

for (line=1;line<=s;line++)
{
	for (loop=1;loop<=s;loop++)
		cout<<" ";
	cin>>ch;
	cout<<endl;
}

It should be this :

for (int i = 0; i < sideLength; i++)
{
	for (int j = 0; j < sideLength; j++)
		cout << ch <<  " ";

	cout<<endl;
}

Edited 6 Years Ago by firstPerson: n/a


This is completely wrong :

Agree

It should be this :

Nope, that's wrong too.


That fuinction requires three loops, as I previously explained.

yES , i DID but no change.

void Square::draw(char ch)
{
    for (int line=0;line<=sidelength-1;line++)
    {
        if(line == 0 || line == sidelength-1)
        {
            // draw the whole line
            for (int loop=1;loop<=sidelength;loop++)
                cout<<ch;
        }
        else
        {
            // draw inside line
            cout<<ch;

            for (int loop=1;loop<=sidelength-2;loop++)
                cout<<" ";

            cout<<ch;
        }

        cout<<endl;
    }
}

I tested this version and now it works, i hope it will help you.

Comments
Gee, i hope you get a good grade on his homework...
This article has been dead for over six months. Start a new discussion instead.