0

im a newbie to c++ and im trying to calling a function of a class in another one but the output just doesnt come out right. I tried to calculate the total cost repeatedly according to different areas, but the output always garbled. i think i have declared it? This is my code:

#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<iomanip>
#include<numeric>
#include<conio.h>
using namespace std;
class RoomDimension
{
public:
        double width;
        double length;
        double area;
        double getarea(void);
        
    void room(int,int);
};
double RoomDimension::getarea()
{
    return area;
}
class RoomCarpet: public RoomDimension
{
    public:
double costpermeter;
double totalcost=0;
double gettotalcost(void);
void calculation();
};
double RoomCarpet ::gettotalcost()
{
    return totalcost;
}
void RoomDimension::room(int n, int m)
{
    RoomCarpet rc;
    cout<<"Number of rooms= ";
    cin>>m;
    if(m<1)
    {
        cout<<"Invalid input."<<endl;
    }
    for (n = 0;n < m;++n)
    {
        cout<<"Length of the room= ";
        cin>>length;
        if(length==0)
        {
            cout<<"Invalid input.";
            return;
        }
        cout<<"Width of the room= ";
        cin>>width;
        if(width==0)
        {
            cout<<"Invalid input.";
            return;
        }
        area=width*length;
        cout<<"Area of the room= "<<area<<endl;
        rc.calculation();
}
    return;
}
void RoomCarpet::calculation()
{
    RoomDimension rd;
    totalcost=0;
    cout<<"Price per square meter= ";
    cin>>costpermeter;
    if(costpermeter==0)
    {
        cout<<"Invalid input."<<endl;
        return;
    }
    double totalcost=costpermeter*rd.area;
    cout<<"Total price= RM"<<totalcost<<endl;
    return;
}
int main()
{
    RoomDimension rd;
    rd.room(0,0);
    return 0;
}

and the output keep showing like this

 Number of rooms= 2
Length of the room= 5
Width of the room= 4
Area of the room= 20
Price per square meter= 5
Total price= RM5.03097e-317
Length of the room= 3
Width of the room= 7
Area of the room= 21
Price per square meter= 7
Total price= RM7.04336e-317

what should i do to fix that?

  • Your code says "a carpet is a kind of dimension" and "a dimension is a thing that can create carpets", neither of which makes much sense to me. – molbdnilo Oct 29 '20 at 08:27

1 Answers1

0

When I compile your code using MSVC, I get two warnings and an error:

  • line 29: Warning C26495 Variable 'RoomCarpet::costpermeter' is uninitialized. Always initialize a member variable (type.6).
  • line 76: Warning C6001 Using uninitialized memory 'rd'.
  • line 76: Error C4700 uninitialized local variable 'rd' used

Especially the last one is a problem

In the function RoomCarpet::calculation, you make (at least) two mistakes:

  • you declare a local variable rd, which is never initialized.
  • you declare and use a local variable totalcost, which hides the class member Roomcarpet::totalcost.

But that problem also occurs in RoomDimension::room, where you start by declaring local variable rc, but you never assign anything to it. When you then call rc.calculation();, you do calculations on unsigned variables.

You should rethink you design. Why is RoomCarpet a derivative of RoomDimension?

JHBonarius
  • 10,824
  • 3
  • 22
  • 41