Thread: help w/another program!!!

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    Question help w/another program!!!

    i have this object program and it basically works but when it returns length, area, etc it gives me a negative totally incorrect answer help??!!
    for more info look at thread called Rectangle class

    // rectangle.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    class Rectangle{
    public:Rectangle();
    Rectangle(float urx, float ury, float ulx, float uly, float llx,float lly, float lrx, float lry);
    void calclength(float urx, float ulx);
    void calcwidth(float ury, float lry);
    float area();
    float perimeter();

    void setlength(float lrx, float llx);
    void setwidth(float uly, float lly);
    void print();
    float getlength();
    float getwidth();
    bool square(float urx, float ury, float lrx, float lry);

    private:float upprx, uppry, upplx, upply, lowrx, lowry,length, width, lowlx, lowly;
    };
    Rectangle:: Rectangle(){
    lowlx=1.0;
    lowly=1.0;
    upplx=1.0;
    upply=2.0;
    lowrx=2.0;
    lowry=1.0;
    upprx=2.0;
    uppry=2.0;}
    Rectangle:: Rectangle(float urx, float ury, float ulx, float uly, float llx,
    float lly, float lrx, float lry){
    lowlx=llx; lowly=lly; lowrx=lrx; lowry=lry; upplx=ulx; upply=uly; upprx=urx;
    uppry=ury;}
    void Rectangle:: calclength(float urx, float lrx){
    length=urx-lrx;}
    void Rectangle:: calcwidth(float ury, float lry){
    width=ury-lry;}
    float Rectangle:: area(){
    return (length*width);}
    float Rectangle:: perimeter(){
    return (2*length+2*width);}

    void Rectangle:: setlength(float lrx, float llx){
    if( lrx<0.0||lrx>20.0)
    lowrx=2;
    else
    lowrx=lrx;
    if(llx<0.0||llx>20.0)
    lowlx=1;
    else
    lowlx=llx;}

    void Rectangle:: setwidth(float uly, float lly){
    if(uly<0.0||uly>20.0)
    upply=2;
    else
    upply=uly;
    if(lly<0.0||lly>20.0)
    lowly=1;
    else
    lowly=lly;}

    float Rectangle:: getlength(){
    return length;}
    float Rectangle:: getwidth(){
    return width;}
    void Rectangle:: print(){
    cout <<"Rectangle length = "<<length<< " and width = "<<width<<endl;}
    bool Rectangle:: square(float urx, float uly, float lrx, float lry){
    if(urx==lrx&&lry==uly){
    cout <<"This is not a rectangle it is a square"<<endl;}
    return 0;}
    int main(int argc, char* argv[])
    {
    Rectangle coor;
    Rectangle rect(2.0, 2.0, 2.0, 6.0, 9.0, 2.0, 9.0, 6.0);
    Rectangle rect2(6.0,2.0, 6.0,6.0,10.0,2.0,10.0,6.0);
    coor.setlength(0,0);
    coor.setwidth(0,0);
    coor.getlength();
    coor.getwidth();
    rect.setlength(9.0,2.0);
    rect.setwidth(6.0,2.0);
    rect.getlength();
    rect.getwidth();
    coor.print();
    coor.square(0,0,0,0);
    rect.print();
    rect2.setlength(10.0,6.0);
    rect2.setwidth(6.0,2.0);
    rect2.getlength();
    rect2.getwidth();
    cout<<"The area is "<< rect.area()<<endl;
    rect2.print();
    cout<<"The perimeter is "<<rect2.perimeter()<<endl;
    return 0;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you should call calc_length and calc_width from your constructor after assigning the other variables. At present your width and length are uninitialised. Make the necessary adjustments to your constructors and all should be well
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    thanx it worked i knew it was going to be something simple but i just couldnt figure it out
    anyway THANX!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM