mercredi 3 juillet 2019

Returning random value instead of expected values! c++

Whenever i ask for the area or perimeter, it returns some absurd values. i tried fixing it for 2 days but it is the same! The code and the class is given below with the output!

I expect the output to be 100 but it shows

enter image description here

252134578 <<<< this

maincode :

#include <iostream>
#include "rect_class.hpp"
using namespace std;

int main()
{
rectangle rect;
int width= 10,height = 10,choice,newwidth,newheight;
bool loop = false;
while(loop == false){
cout << endl << endl;
cout << " *** Menu *** " << endl;
cout << "(1) Draw Rectangle" << endl;
cout << "(2) Area" << endl;
cout << "(3) Perimeter" << endl;
cout << "(4) Resize" << endl;
cout << "(5) Quit" << endl;
cout << "Enter your choice :";
cin >> choice;
cout << endl;

switch(choice){
case 2 :cout << rect.getArea();
        break;
case 3 : cout << rect.getPerimeter();
        break;
case 4 : cout << "enter your height : ";
             cin >> newheight;
             cout << "enter your width : ";
             cin >> newwidth;
             rect.setHeight(newheight);
             rect.setWidth(newwidth);
    break;
case 5 : loop = true;
        cout << "exiting...";
        break;
default: cout << "bro type the menu nums !!";
        break;
}
};

rect_class.hpp

class rectangle {
public :
  int getHeight() const {return itsHeight;}  //accessors
  int getWidth() const {return itsWidth;}
  void setHeight(int height){itsHeight = height;}
  void setWidth(int width){itsWidth = width;}
  int getArea();
  int getPerimeter();
private :
    int itsHeight;
    int itsWidth;
};

int rectangle::getArea(){
    return itsWidth*itsHeight;
};

int rectangle::getPerimeter(){
    return 2*(itsWidth*itsHeight);
};

I am in the beginning years of my programming journey,so sorry for any silly mistakes! :-)

Aucun commentaire:

Enregistrer un commentaire