How to divide Double?

I am doing this code:

double getArea()//this is for a rectangle.
{

double area;
area = getLength * getWidth;
return area;

}

I keep getting this error: '*': illegal, left operand has type double.
How do i mulitpy doubles? My instructor states that I have to multiply each the getLength and the getWith values, but I don't know how without getting the errors.

Please help?
well if you get the area using a function double getArea(), then I'll bet that
getWidth and getLength are functions too.

so maybe?:
area = getLength() * getWidth();
Last edited on
I tried your suggestion and it works. I no longer get any errors.

I think however that it is calling the getLength() and the getWidth() function.

They ask for the length and the width of the rectangle twice. So by putting your suggestion it is actually asking the user twice to enter the values.
Last edited on
Here is the whole code:

#include <iostream>

#include <iomanip> 

using namespace std; 

double getLength() //getting the length of a rectangle. 
{

double lenght; 
cout << "Enter the Lenght of the rectangle: "; 
cin >> lenght;

return lenght; 
}

double getWidth()//getting the width of a rectangle 
{

double width; 
cout <<"Enter the Width of the rectangle: "; 
cin >> width;

return width; 
}

double getArea() //getting the area of a rectangle 
{

double area; 
area =  * ;     //here is the part where I need to multiply


return area; 

}

void displayData(double length, double width, double area)//displaying data 
{


cout << "The length of the rectangle is: " << length << endl; 
cout << "The width of the rectangle is: " << width << endl; 
cout << "The area of the rectangle is: " << area << endl; 

}

int main() 
{


double length, // The rectangle's length, width, area.
       width, 
       area; 





// Get the rectangle's length. 

length = getLength();



// Get the rectangle's width. 

width = getWidth();



// Get the rectangle's area. 
area = getArea();



// Display the rectangle's data. 

displayData(length, width, area);



return 0; 
Try to put your code between code tags. The code tag(s) is the # symbol in the little tool box to the right of the edit window when you are making a post.

Your 'problem' is straight forward.
First you are asking the user for the Length then the Width and you are storing
the values in LOCAL variables in the main function.
You have a getArea function that takes no parameters - so the getArea function does not get the Length and Width values in order to calculate the area.

You can do the following.
Make two overloaded versions of the getArea function. One version takes no parameters and will ask the user for the Length and Width values.
The other version will take two parameters - one for the length and one for the Width.
You can then choose which version to use depending on whether you already have the Length/Width values or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
//Overloading the getArea function

//this version if you already have the length & width values
double getArea(double length, double width) 
{
    return length * width;
}

//this version if you  want to get the length and with values from the user
double getArea() 
{
    return getLength() * getWidth();
}


**EDIT**
But I just realised thet this overloading could cause a problem with the displayData(length, width, area); function because you need the length and width.
So it would be best to stick with just the
1
2
3
4
double getArea(double length, double width) 
{
    return length * width;
}

So your main function will look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    double length, // The rectangle's length, width, area.
    width,
    area;
// Get the rectangle's length.
    length = getLength();
// Get the rectangle's width.
    width = getWidth();
// Get the rectangle's area.
    area = getArea(length, width); //<======

// Display the rectangle's data.
    displayData(length, width, area);
  


Last edited on
Thank you very much. I used your code and it works like a charm. I had tried this before, but I was forgetting the length and width in the main.

1
2
3
4
5
6
7
8
9
10
11
12
double getArea(double length, double width) 
{
    return length * width;
}

int main()
{
    
// Get the rectangle's area.
    area = getArea(length, width); //<====== was forgetting to add this.

Topic archived. No new replies allowed.