473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Teaching Myself C++ and getting stuck with a few syntax problems

Greetings,

I have been trying to teach myself C++ over the past few weeks and
have finally came across a problem I could not fix. I made a simple
program that prints out a square or rectangle using the * character.
The program was just for practice but I am having problems.

My main problem is, in my program I use 4 functions to change or
access two variables in my code. The variables are

itsHeight;
itsWidth;

The functions are
getHeight(), getWidth()
and
setHeight(), setWidth()

Now for some reason the value that gets stored in itsHeight and
itsWidth goes haywire and saves an astronomically huge number. It
makes my square huge to where you cant see it on a screen and takes a
long time to print. If I move the two variables outside of the class
and make them global, they work like normal. I know I must be doing
something wrong.

I am also having problems creating prototypes of my functions. I could
not create a prototype of my functions that needed to access my
Rectangle class. I was forced to define all the functions above the
Main and then define an object for each one. I was sure there was a
way to use prototypes so you could define your function later, but
somehow force the function to recognize my class ahead of time. Does
this make sense? Right now only my menu function has a prototype. I
know defining a function before main is perfectly fine and that making
a prototype makes no difference but for the sake of learning, and the
sake of organization any help would rock!

The programs not done and there might be a few things in here that I
am not using at the very moment. That stuff should be commented out. I
also have not defined my variables very well between INT, Unsigneds,
longs and shorts. This is because I had it all nice and neatly
organized to what I think I would need for each function, but then
when I hit this brick wall I started changing them to INT to see if it
would make a difference. I will go back and make the appropriate
changes when I know what im doing wrong. Please lend advise to a newb!

#include <iostream>
using namespace std;

//Define Variable types
typedef unsigned short int USI;
typedef unsigned long int ULI;
enum BOOL {FALSE, TRUE};
enum uchoose {print=1, area, perimeter, resize, salir};

//declare rectangle class
class Rectangle {
private: //Private Class Variables
int itsWidth;
int itsHeight;

public: //Public Variables and Functions
ULI printArea(){return itsHeight*itsWidth;}
ULI printPerimeter(){return itsHeight*2+itsWidth*2;}
int getHeight() const {return itsHeight;}
int getWidth() const {return itsWidth;}
int setHeight(int);
int setWidth(int);
};

int Rectangle::setHeight(int h) {
itsHeight=h;
}

int Rectangle::setWidth(int w) {
itsWidth=w;
}

void getMenu();

int printRect() {
Rectangle rect;
int h=rect.getHeight(),w=rect.getWidth();
for (USI x=0; x<h; x++) {
for (USI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}

}

void setSize() {
Rectangle rect;
int h,w;
cout << "\nInput the Height: ";
cin >h;
rect.setHeight(h);
cout << "\nInput the Width: ";
cin >w;
rect.setWidth(w);
}

void Area() {
Rectangle rect;
cout << rect.printArea();
}

void Perimeter() {
Rectangle rect;
cout << rect.printPerimeter();
}

int main() {
Rectangle rect;
USI choice;
BOOL x=FALSE;
while (!x) {
getMenu();
cin >choice;
switch (choice)
{
case 1:
printRect();
break;
case 2:
Area();
break;
case 3:
Perimeter();
break;
case 4:
setSize();
break;
case 5:
x=TRUE;
break;
}
cout << endl;
}
}

void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}

Jun 21 '07 #1
10 1846

class Rectangle {

public:

// You want a constructor:
Rectangle()
{
itsWidth= 0;
itsHeight=0;
}
};

Jun 21 '07 #2
On 21 Jun, 14:32, "Gernot Frisch" <M...@Privacy.netwrote:
class Rectangle {

public:

// You want a constructor:
Rectangle()
{
itsWidth= 0;
itsHeight=0;

}
};- Hide quoted text -

- Show quoted text -
Or use an initialisation list.

The bigger problem in the code is that Cliff creates a Rectangle
object in his main() loop, but doesn't pass it to his various
functions, instead he creates temporary objects in the functions.
These temporaries obviously bear no relation to that in the main loop.

So, to fix the problem, Cliff, pass your Rectangle object by reference
to your functions. For example,
void printRect(Rectangle& rect); etc.

To clear up your problems with prototyping, perhaps now is a good time
to learn about header files.

HTH.
Thes.

Jun 21 '07 #3
On 2007-06-21 15:19, Cliff wrote:
Greetings,

I have been trying to teach myself C++ over the past few weeks and
have finally came across a problem I could not fix. I made a simple
program that prints out a square or rectangle using the * character.
The program was just for practice but I am having problems.

My main problem is, in my program I use 4 functions to change or
access two variables in my code. The variables are

itsHeight;
itsWidth;

The functions are
getHeight(), getWidth()
and
setHeight(), setWidth()

Now for some reason the value that gets stored in itsHeight and
itsWidth goes haywire and saves an astronomically huge number. It
makes my square huge to where you cant see it on a screen and takes a
long time to print. If I move the two variables outside of the class
and make them global, they work like normal. I know I must be doing
something wrong.

I am also having problems creating prototypes of my functions. I could
not create a prototype of my functions that needed to access my
Rectangle class. I was forced to define all the functions above the
Main and then define an object for each one. I was sure there was a
way to use prototypes so you could define your function later, but
somehow force the function to recognize my class ahead of time. Does
this make sense? Right now only my menu function has a prototype. I
know defining a function before main is perfectly fine and that making
a prototype makes no difference but for the sake of learning, and the
sake of organization any help would rock!

The programs not done and there might be a few things in here that I
am not using at the very moment. That stuff should be commented out. I
also have not defined my variables very well between INT, Unsigneds,
longs and shorts. This is because I had it all nice and neatly
organized to what I think I would need for each function, but then
when I hit this brick wall I started changing them to INT to see if it
would make a difference. I will go back and make the appropriate
changes when I know what im doing wrong. Please lend advise to a newb!

#include <iostream>
using namespace std;

//Define Variable types
typedef unsigned short int USI;
typedef unsigned long int ULI;
Just a tip, for most practical intents and purposes a normal int will do
just fine, or unsigned int if the values are all positive (for values
indicating size I usually use the type size_t which is an unsigned
integer). While it at first might seem like a good idea to use short for
values that will never be large the truth is that on some machines it
takes more time working with a short (since it's not the natural size)
and any space savings can be eaten up by padding. So a good tip is to
not use other types unless you have a good reason. The same is true for
double vs float, double is usually preferable.
enum BOOL {FALSE, TRUE};
No, no no. In C++ we have this wonderful type called bool specially
designed for this.
enum uchoose {print=1, area, perimeter, resize, salir};
You don't use this one.
//declare rectangle class
class Rectangle {
private: //Private Class Variables
int itsWidth;
int itsHeight;

public: //Public Variables and Functions
You probably want to add a constructor which initializes the value when
you create a new rectangle:

Rectangle() : itsWidth(0), itsHeight(0) { }

That's an initialization list, it does the same thing as

itsWidth = 0;
itsHeight = 0;

but is more efficient. It might also be convenient to be able to tell
the size of the rectangle when it's created, so lets add a constructor
for that also.

Rectangle(int w, int h) : itsWidth(w), itsHeight(h) { }

It is possible to combine these two constructors into one using default
parameters, but I leave that as homework for you to look into.
ULI printArea(){return itsHeight*itsWidth;}
ULI printPerimeter(){return itsHeight*2+itsWidth*2;}
int getHeight() const {return itsHeight;}
int getWidth() const {return itsWidth;}
int setHeight(int);
int setWidth(int);
};

int Rectangle::setHeight(int h) {
itsHeight=h;
}

int Rectangle::setWidth(int w) {
itsWidth=w;
}
Out of curiosity, is there some special reason that you chose to define
those two methods outside of the class declaration? Not that it's wrong,
either way works.
void getMenu();

int printRect() {
Rectangle rect;
As Thes said, your problem is that you create a new rectangle in the
function (and since you didn't initialize the values you got weird
results) instead of passing the rectangle to print as an argument to the
function. So rewrite the above two lines to

void printRect(const Rectangle& rect) {

Three things to notice: the first is that since the method does not
return any value I changed the return type to void.
The second thing is the & after Rectangle, this means that the rectangle
is passed as a reference this means that any changes you make in this
function will affect the rectangle you supplies as argument to the
function. If you omit the & you will pass by value, which means that any
changes you make in this function will be made on a copy (which will be
discarded at the end of the function) of the argument, and will not
affect the rectangle passed as argument.
The last thing is the word const, since we pass by reference you could
change the argument, but since we will only print the rectangle we add
the word const which means that 'rect' will be constant (not changeable)
in this function, and the compiler will complain if you try to modify it.
int h=rect.getHeight(),w=rect.getWidth();
for (USI x=0; x<h; x++) {
for (USI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}

}

void setSize() {
Rectangle rect;
Once again, you can replace these two lines, but since we do change the
rectangle in this function you must skip the const. I'm sure you by now
can figure out the last two.
int h,w;
cout << "\nInput the Height: ";
cin >h;
rect.setHeight(h);
cout << "\nInput the Width: ";
cin >w;
rect.setWidth(w);
}

void Area() {
Rectangle rect;
cout << rect.printArea();
}

void Perimeter() {
Rectangle rect;
cout << rect.printPerimeter();
}

int main() {
Rectangle rect;
If you want to set some specific size use the constructor like so:

Rectangle rect(10, 10); // A 10 by 10 rectangle
USI choice;
BOOL x=FALSE;
Here I would go
size_t choice;
bool x = false;

While the usage of size_t is mostly a matter of style and opinion you
really should be using the built-in type bool for x.
while (!x) {
getMenu();
cin >choice;
switch (choice)
{
case 1:
printRect();
Since we have a Rectangle object and have modified our functions we can
now use them by passing that object to the functions:

printRect(rect);
break;
case 2:
Area();
break;
case 3:
Perimeter();
break;
case 4:
setSize();
break;
case 5:
x=TRUE;
break;
}
cout << endl;
}
}

void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}
--
Erik Wikström
Jun 21 '07 #4
Thanks for the help guys! I cant beleive the amount of help I got. I
might have to stop in from time to time for more guidance. I probably
do alot of things that wouldnt be conciderd "good programming". It's
hard when your teaching yourself but I find this to be a fun past time
and a fun hobby.

Again thanks for the help I'll post again if I hit another roadblock!

Cliff

On Jun 21, 9:55 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-21 15:19, Cliff wrote:


Greetings,
I have been trying to teach myself C++ over the past few weeks and
have finally came across a problem I could not fix. I made a simple
program that prints out a square or rectangle using the * character.
The program was just for practice but I am having problems.
My main problem is, in my program I use 4 functions to change or
access two variables in my code. The variables are
itsHeight;
itsWidth;
The functions are
getHeight(), getWidth()
and
setHeight(), setWidth()
Now for some reason the value that gets stored in itsHeight and
itsWidth goes haywire and saves an astronomically huge number. It
makes my square huge to where you cant see it on a screen and takes a
long time to print. If I move the two variables outside of the class
and make them global, they work like normal. I know I must be doing
something wrong.
I am also having problems creating prototypes of my functions. I could
not create a prototype of my functions that needed to access my
Rectangle class. I was forced to define all the functions above the
Main and then define an object for each one. I was sure there was a
way to use prototypes so you could define your function later, but
somehow force the function to recognize my class ahead of time. Does
this make sense? Right now only my menu function has a prototype. I
know defining a function before main is perfectly fine and that making
a prototype makes no difference but for the sake of learning, and the
sake of organization any help would rock!
The programs not done and there might be a few things in here that I
am not using at the very moment. That stuff should be commented out. I
also have not defined my variables very well between INT, Unsigneds,
longs and shorts. This is because I had it all nice and neatly
organized to what I think I would need for each function, but then
when I hit this brick wall I started changing them to INT to see if it
would make a difference. I will go back and make the appropriate
changes when I know what im doing wrong. Please lend advise to a newb!
#include <iostream>
using namespace std;
//Define Variable types
typedef unsigned short int USI;
typedef unsigned long int ULI;

Just a tip, for most practical intents and purposes a normal int will do
just fine, or unsigned int if the values are all positive (for values
indicating size I usually use the type size_t which is an unsigned
integer). While it at first might seem like a good idea to use short for
values that will never be large the truth is that on some machines it
takes more time working with a short (since it's not the natural size)
and any space savings can be eaten up by padding. So a good tip is to
not use other types unless you have a good reason. The same is true for
double vs float, double is usually preferable.
enum BOOL {FALSE, TRUE};

No, no no. In C++ we have this wonderful type called bool specially
designed for this.
enum uchoose {print=1, area, perimeter, resize, salir};

You don't use this one.
//declare rectangle class
class Rectangle {
private: //Private Class Variables
int itsWidth;
int itsHeight;
public: //Public Variables and Functions

You probably want to add a constructor which initializes the value when
you create a new rectangle:

Rectangle() : itsWidth(0), itsHeight(0) { }

That's an initialization list, it does the same thing as

itsWidth = 0;
itsHeight = 0;

but is more efficient. It might also be convenient to be able to tell
the size of the rectangle when it's created, so lets add a constructor
for that also.

Rectangle(int w, int h) : itsWidth(w), itsHeight(h) { }

It is possible to combine these two constructors into one using default
parameters, but I leave that as homework for you to look into.
ULI printArea(){return itsHeight*itsWidth;}
ULI printPerimeter(){return itsHeight*2+itsWidth*2;}
int getHeight() const {return itsHeight;}
int getWidth() const {return itsWidth;}
int setHeight(int);
int setWidth(int);
};
int Rectangle::setHeight(int h) {
itsHeight=h;
}
int Rectangle::setWidth(int w) {
itsWidth=w;
}

Out of curiosity, is there some special reason that you chose to define
those two methods outside of the class declaration? Not that it's wrong,
either way works.
void getMenu();
int printRect() {
Rectangle rect;

As Thes said, your problem is that you create a new rectangle in the
function (and since you didn't initialize the values you got weird
results) instead of passing the rectangle to print as an argument to the
function. So rewrite the above two lines to

void printRect(const Rectangle& rect) {

Three things to notice: the first is that since the method does not
return any value I changed the return type to void.
The second thing is the & after Rectangle, this means that the rectangle
is passed as a reference this means that any changes you make in this
function will affect the rectangle you supplies as argument to the
function. If you omit the & you will pass by value, which means that any
changes you make in this function will be made on a copy (which will be
discarded at the end of the function) of the argument, and will not
affect the rectangle passed as argument.
The last thing is the word const, since we pass by reference you could
change the argument, but since we will only print the rectangle we add
the word const which means that 'rect' will be constant (not changeable)
in this function, and the compiler will complain if you try to modify it.
int h=rect.getHeight(),w=rect.getWidth();
for (USI x=0; x<h; x++) {
for (USI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}
}
void setSize() {
Rectangle rect;

Once again, you can replace these two lines, but since we do change the
rectangle in this function you must skip the const. I'm sure you by now
can figure out the last two.


int h,w;
cout << "\nInput the Height: ";
cin >h;
rect.setHeight(h);
cout << "\nInput the Width: ";
cin >w;
rect.setWidth(w);
}
void Area() {
Rectangle rect;
cout << rect.printArea();
}
void Perimeter() {
Rectangle rect;
cout << rect.printPerimeter();
}
int main() {
Rectangle rect;

If you want to set some specific size use the constructor like so:

Rectangle rect(10, 10); // A 10 by 10 rectangle
USI choice;
BOOL x=FALSE;

Here I would go
size_t choice;
bool x = false;

While the usage of size_t is mostly a matter of style and opinion you
really should be using the built-in type bool for x.
while (!x) {
getMenu();
cin >choice;
switch (choice)
{
case 1:
printRect();

Since we have a Rectangle object and have modified our functions we can
now use them by passing that object to the functions:

printRect(rect);


break;
case 2:
Area();
break;
case 3:
Perimeter();
break;
case 4:
setSize();
break;
case 5:
x=TRUE;
break;
}
cout << endl;
}
}
void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}

--
Erik Wikström- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Jun 21 '07 #5
On Jun 21, 10:33 am, Cliff <Cliff.Ros...@gmail.comwrote:
Thanks for the help guys! I cant beleive the amount of help I got. I
might have to stop in from time to time for more guidance. I probably
do alot of things that wouldnt be conciderd "good programming". It's
hard when your teaching yourself but I find this to be a fun past time
and a fun hobby.

Again thanks for the help I'll post again if I hit another roadblock!

Cliff

On Jun 21, 9:55 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-21 15:19, Cliff wrote:
Greetings,
I have been trying to teach myself C++ over the past few weeks and
have finally came across a problem I could not fix. I made a simple
program that prints out a square or rectangle using the * character.
The program was just for practice but I am having problems.
My main problem is, in my program I use 4 functions to change or
access two variables in my code. The variables are
itsHeight;
itsWidth;
The functions are
getHeight(), getWidth()
and
setHeight(), setWidth()
Now for some reason the value that gets stored in itsHeight and
itsWidth goes haywire and saves an astronomically huge number. It
makes my square huge to where you cant see it on a screen and takes a
long time to print. If I move the two variables outside of the class
and make them global, they work like normal. I know I must be doing
something wrong.
I am also having problems creating prototypes of my functions. I could
not create a prototype of my functions that needed to access my
Rectangle class. I was forced to define all the functions above the
Main and then define an object for each one. I was sure there was a
way to use prototypes so you could define your function later, but
somehow force the function to recognize my class ahead of time. Does
this make sense? Right now only my menu function has a prototype. I
know defining a function before main is perfectly fine and that making
a prototype makes no difference but for the sake of learning, and the
sake of organization any help would rock!
The programs not done and there might be a few things in here that I
am not using at the very moment. That stuff should be commented out. I
also have not defined my variables very well between INT, Unsigneds,
longs and shorts. This is because I had it all nice and neatly
organized to what I think I would need for each function, but then
when I hit this brick wall I started changing them to INT to see if it
would make a difference. I will go back and make the appropriate
changes when I know what im doing wrong. Please lend advise to a newb!
#include <iostream>
using namespace std;
//Define Variable types
typedef unsigned short int USI;
typedef unsigned long int ULI;
Just a tip, for most practical intents and purposes a normal int will do
just fine, or unsigned int if the values are all positive (for values
indicating size I usually use the type size_t which is an unsigned
integer). While it at first might seem like a good idea to use short for
values that will never be large the truth is that on some machines it
takes more time working with a short (since it's not the natural size)
and any space savings can be eaten up by padding. So a good tip is to
not use other types unless you have a good reason. The same is true for
double vs float, double is usually preferable.
enum BOOL {FALSE, TRUE};
No, no no. In C++ we have this wonderful type called bool specially
designed for this.
enum uchoose {print=1, area, perimeter, resize, salir};
You don't use this one.
//declare rectangle class
class Rectangle {
private: //Private Class Variables
int itsWidth;
int itsHeight;
public: //Public Variables and Functions
You probably want to add a constructor which initializes the value when
you create a new rectangle:
Rectangle() : itsWidth(0), itsHeight(0) { }
That's an initialization list, it does the same thing as
itsWidth = 0;
itsHeight = 0;
but is more efficient. It might also be convenient to be able to tell
the size of the rectangle when it's created, so lets add a constructor
for that also.
Rectangle(int w, int h) : itsWidth(w), itsHeight(h) { }
It is possible to combine these two constructors into one using default
parameters, but I leave that as homework for you to look into.
ULI printArea(){return itsHeight*itsWidth;}
ULI printPerimeter(){return itsHeight*2+itsWidth*2;}
int getHeight() const {return itsHeight;}
int getWidth() const {return itsWidth;}
int setHeight(int);
int setWidth(int);
};
int Rectangle::setHeight(int h) {
itsHeight=h;
}
int Rectangle::setWidth(int w) {
itsWidth=w;
}
Out of curiosity, is there some special reason that you chose to define
those two methods outside of the class declaration? Not that it's wrong,
either way works.
void getMenu();
int printRect() {
Rectangle rect;
As Thes said, your problem is that you create a new rectangle in the
function (and since you didn't initialize the values you got weird
results) instead of passing the rectangle to print as an argument to the
function. So rewrite the above two lines to
void printRect(const Rectangle& rect) {
Three things to notice: the first is that since the method does not
return any value I changed the return type to void.
The second thing is the & after Rectangle, this means that the rectangle
is passed as a reference this means that any changes you make in this
function will affect the rectangle you supplies as argument to the
function. If you omit the & you will pass by value, which means that any
changes you make in this function will be made on a copy (which will be
discarded at the end of the function) of the argument, and will not
affect the rectangle passed as argument.
The last thing is the word const, since we pass by reference you could
change the argument, but since we will only print the rectangle we add
the word const which means that 'rect' will be constant (not changeable)
in this function, and the compiler will complain if you try to modify it.
int h=rect.getHeight(),w=rect.getWidth();
for (USI x=0; x<h; x++) {
for (USI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}
}
void setSize() {
Rectangle rect;
Once again, you can replace these two lines, but since we do change the
rectangle in this function you must skip the const. I'm sure you by now
can figure out the last two.
int h,w;
cout << "\nInput the Height: ";
cin >h;
rect.setHeight(h);
cout << "\nInput the Width: ";
cin >w;
rect.setWidth(w);
}
void Area() {
Rectangle rect;
cout << rect.printArea();
}
void Perimeter() {
Rectangle rect;
cout << rect.printPerimeter();
}
int main() {
Rectangle rect;
If you want to set some specific size use the constructor like so:
Rectangle rect(10, 10); // A 10 by 10 rectangle
USI choice;
BOOL x=FALSE;
Here I would go
size_t choice;
bool x = false;
While the usage of size_t is mostly a matter of style and opinion you
really should be using the built-in type bool for x.
while (!x) {
getMenu();
cin >choice;
switch (choice)
{
case 1:
printRect();
Since we have a Rectangle object and have modified our functions we can
now use them by passing that object to the functions:
printRect(rect);
break;
case 2:
Area();
break;
case 3:
Perimeter();
break;
case 4:
setSize();
break;
case 5:
x=TRUE;
break;
}
cout << endl;
}
}
void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}
--
Erik Wikström- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
Based on your guyses advise here is what I have

#include <iostream>
using namespace std;

//Define Variable types
typedef unsigned int UI;
enum uchoose {print=1, area, perimeter, resize, salir};

//declare rectangle class
class Rectangle {
private:
UI itsWidth;
UI itsHeight;

public: //Public Variables and Functions
Rectangle(UI h, UI w);
~Rectangle();
UI printArea(){return itsHeight*itsWidth;}
UI printPerimeter(){return itsHeight*2+itsWidth*2;}
int setHeight(int h) {itsHeight=h;}
int setWidth(int w) {itsWidth=w;}
int getHeight() const {return itsHeight;}
int getWidth() const {return itsWidth;}
};

Rectangle::Rectangle(UI height, UI width){
itsHeight=height;
itsWidth=width;
}

Rectangle::~Rectangle() {}

void getMenu();
void printRect(Rectangle);
void Area(Rectangle);
void Perimeter(Rectangle);
void setSize(Rectangle);

int main() {
bool x=false;
Rectangle rect(2,2);
UI choice;
while (!x) {
getMenu();
cin >choice;
switch (choice)
{
case print:
printRect(rect);
break;
case area:
Area(rect);
break;
case perimeter:
Perimeter(rect);
break;
case resize:
setSize(rect);
break;
case salir:
x=true;
break;
}
cout << endl;
}
}

void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}

void printRect(Rectangle rect) {
int h=rect.getHeight(),w=rect.getWidth();
for (UI x=0; x<h; x++) {
for (UI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}
}

void Area(Rectangle rect) {
cout << rect.printArea();
}

void Perimeter(Rectangle rect) {
cout << rect.printPerimeter();
}

void setSize(Rectangle rect){
int h,w;
cout << "\nInput the Height: ";
cin >h;
cout << "\nInput the Width: ";
cin >w;
rect.setHeight(h);
rect.setWidth(w);
}
I still am having problems with the output of hte square but a least I
learned how to pass a referance of a class through the variables. When
I get home I look closer to how I pass my values through the
variables. I need to get more information on how making certain things
a "const" benefits your program. Any how its all learning.

Thanks for the help guys!

Jun 21 '07 #6
void setSize(Rectangle rect){
int h,w;
cout << "\nInput the Height: ";
cin >h;
cout << "\nInput the Width: ";
cin >w;
rect.setHeight(h);
rect.setWidth(w);
}
I still am having problems with the output of hte square but a least I
learned how to pass a referance of a class through the variables. When
I get home I look closer to how I pass my values through the
variables. I need to get more information on how making certain things
a "const" benefits your program. Any how its all learning.

Thanks for the help guys!
setSize is wrong. The rect parameter is a *copy* of the rectangle you
created in main. You change the size of the copy, but the original in
main is untouched. You must pass the parameter by reference

void setSize(Rectangle& rect){ // the & is really important

Now setSize will change the size of the rectangle passed to it, not to a
copy of the rectangle.

john
Jun 21 '07 #7
On 2007-06-21 17:33, Cliff wrote:
Thanks for the help guys! I cant beleive the amount of help I got. I
might have to stop in from time to time for more guidance. I probably
do alot of things that wouldnt be conciderd "good programming". It's
hard when your teaching yourself but I find this to be a fun past time
and a fun hobby.

Again thanks for the help I'll post again if I hit another roadblock!
If you do post again, take care to avoid top-post, it is generally
frowned upon. You can also take some time to read through section 5 of
the FAQ: http://www.parashift.com/c++-faq-lite/how-to-post.html

Also, a good way of learning is by using a good book. Many here will
recommend Accelerated C++ by Koenig and Moo, which will teach you "good
C++" from the start.

--
Erik Wikström
Jun 21 '07 #8
On Jun 21, 12:50 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-21 17:33, Cliff wrote:
Thanks for the help guys! I cant beleive the amount of help I got. I
might have to stop in from time to time for more guidance. I probably
do alot of things that wouldnt be conciderd "good programming". It's
hard when your teaching yourself but I find this to be a fun past time
and a fun hobby.
Again thanks for the help I'll post again if I hit another roadblock!

If you do post again, take care to avoid top-post, it is generally
frowned upon. You can also take some time to read through section 5 of
the FAQ:http://www.parashift.com/c++-faq-lite/how-to-post.html

Also, a good way of learning is by using a good book. Many here will
recommend Accelerated C++ by Koenig and Moo, which will teach you "good
C++" from the start.

--
Erik Wikström
ahh ok gotcha, will post on the bottom for now. Ill go read the faq
now.

Yea I realise teaching myself is probably not the best way to go about
this. I have found many useful resources on lines and small books
through torrents and what not. My problem is I am a Peace Corp
Volunteer in Honduras and all the books here are in spanish, what few
books there are... I could get one shipped to me but it is very
expensive.

Any how I got the program to work, I had to use the referance symnol &
when passing the rectangle class through the functions to make sure I
was using the same copy of a rectangle I guess.

Thanks

Jun 21 '07 #9
Cliff wrote:
Thanks for the help guys!

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
Jun 21 '07 #10
"Cliff" <Cl**********@gmail.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
On Jun 21, 10:33 am, Cliff <Cliff.Ros...@gmail.comwrote:
Thanks for the help guys! I cant beleive the amount of help I got. I
might have to stop in from time to time for more guidance. I probably
do alot of things that wouldnt be conciderd "good programming". It's
hard when your teaching yourself but I find this to be a fun past time
and a fun hobby.

Again thanks for the help I'll post again if I hit another roadblock!
[SNIP old code]
>
Based on your guyses advise here is what I have

#include <iostream>
using namespace std;

//Define Variable types
typedef unsigned int UI;
enum uchoose {print=1, area, perimeter, resize, salir};

//declare rectangle class
class Rectangle {
private:
UI itsWidth;
UI itsHeight;

public: //Public Variables and Functions
Rectangle(UI h, UI w);
~Rectangle();
UI printArea(){return itsHeight*itsWidth;}
UI printPerimeter(){return itsHeight*2+itsWidth*2;}
int setHeight(int h) {itsHeight=h;}
Personal choice, does the user care if it's set or get? And you aren't
returning a value, so why have a return value of int? In the least make it:
void setHeight( int h) { itsHeight=h; }
but, personally, I would just do:
void Height( int h ) { itsHeight = h; }
int setWidth(int w) {itsWidth=w;}
Same here.
void Width( int w ) { itsWidth = w; }
int getHeight() const {return itsHeight;}
Okay, now we actually do return an int, and it is const. But I would still
rename it (personal choice).
int Height() const { return itsHeight; }
int getWidth() const {return itsWidth;}
Again.
int Width() const { return itsWidth; }
};

Rectangle::Rectangle(UI height, UI width){
itsHeight=height;
itsWidth=width;
}

Rectangle::~Rectangle() {}

void getMenu();
void printRect(Rectangle);
void Area(Rectangle);
void Perimeter(Rectangle);
void setSize(Rectangle);

int main() {
bool x=false;
Rectangle rect(2,2);
UI choice;
while (!x) {
getMenu();
cin >choice;
If someone enters a letter here instead of a number you'll go into an
endless loop. look at std::cin.ignore()
switch (choice)
{
case print:
printRect(rect);
break;
case area:
Area(rect);
break;
case perimeter:
Perimeter(rect);
break;
case resize:
setSize(rect);
break;
case salir:
x=true;
break;
}
cout << endl;
}
}

void getMenu() {
cout << "Print the Rectangle\t(1)\n";
cout << "View the Area\t\t(2)\n";
cout << "View the Perimeter\t(3)\n";
cout << "Resize\t\t\t(4)\n";
cout << "Exit\t\t\t(5)\n";
}

void printRect(Rectangle rect) {
You are passing a copy of the Rectangle here. Since you're not chaning it,
that's okay, but then it's not const correct either. A const reference
would be better, however, so you wouldn't have to waste time copying.

void printRect( const Rectangle& rect ) {
int h=rect.getHeight(),w=rect.getWidth();
Personal choice. I'll only put one assignment on it's own line so I would
make this:
int h = rect.Height();
int w = rect.Width();
( remember I would change the method names).
for (UI x=0; x<h; x++) {
for (UI y=0; y<w; y++) {
cout << "*";
}
cout << endl;
}
}

void Area(Rectangle rect) {
Again.
void Area( const Rectangle& rect ) {
cout << rect.printArea();
}

void Perimeter(Rectangle rect) {
void Perimeter( const Rectangle& rect ) {
cout << rect.printPerimeter();
}

void setSize(Rectangle rect){
Problem. You are passing the Rectangle by value, that is, a copy. This
makes a local copy of Rectangle that will only be visible in this method.
So here we absolutely need a reference, but this time we are going to change
it, so do not make it const.

void setSize( Rectangle& rect ) {
int h,w;
cout << "\nInput the Height: ";
cin >h;
cout << "\nInput the Width: ";
cin >w;
Enter a character for h and you get garbage for height and width. Enter a
character for width, you get garbage for width. In the least, initialize h
and w to something (such as 0) so at least a known value will be entered.
At most, check for error conditions and look at std::cin.ignore();
rect.setHeight(h);
Since I would of renamed the methods, I would simply do:
rect.Height( h );
rect.setWidth(w);
Again, personal choice.
rect.Width( w );
}

I still am having problems with the output of hte square but a least I
learned how to pass a referance of a class through the variables. When
I get home I look closer to how I pass my values through the
variables. I need to get more information on how making certain things
a "const" benefits your program. Any how its all learning.

Thanks for the help guys!

Jun 23 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mediocre Person | last post by:
Well, after years of teaching grade 12 students c++, I've decided to make a switch to Python. Why? * interactive mode for learning * less fussing with edit - compile - link - run - debug -...
14
by: Gabriel Zachmann | last post by:
This post is not strictly Python-specific, still I would like to learn other university teachers' opinion. Currently, I'm teaching "introduction to OO programming" at the undergrad level. My...
822
by: Turamnvia Suouriviaskimatta | last post by:
I 'm following various posting in "comp.lang.ada, comp.lang.c++ , comp.realtime, comp.software-eng" groups regarding selection of a programming language of C, C++ or Ada for safety critical...
6
by: Ken Kast | last post by:
Here's my situation. I have an object function Obj () { this.foo = null; } a function function bar() {...} another function
12
by: Pierre Senellart | last post by:
I am going to teach a basic Web design course (fundamentals of HTML/CSS, plus some basic client-side (JavaScript) and server-side (PHP, perhaps XSLT) scripting). Most of the students do not have...
2
by: Michael Powell | last post by:
Hi, I have been teaching myself C# and ASP, i wrote a sign-in and register program to teach myself the use of SQL and databases using C# as the development language. Yesterday my little program...
24
by: Richard Aubin | last post by:
I'm really new to vb.net programming and programming in general. I would like to teach myself on how to program effectively and I have the financial and time resources to do so. Can I anyone...
2
by: viki | last post by:
trying to query farmers list database, with a field with individual farmers numbers and want the query to just be farmers numbers beginning with different letters of the alphabet, eg A. Keep...
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.