C
Cliff
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";
}
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";
}