I am an absolute noob to programming and i'm having some serious problems. This is a problem i have for my programming class and I'm stumped. It's saying a have to return a value to the main from a function but i just don't understand why or why it's won't compile. PLEASE help! :(
//************************************
//* This program calculates the area
//* of a rectangle.
//************************************
#include <iostream>
usingnamespace std;
double getLength();
double getWidth();
double getArea(double, double);
double displayData (double, double, double);
int main ()
{
double rect_length,
rect_width,
rect_area;
rect_length= getLength();
rect_width= getWidth();
rect_area=getArea(rect_length, rect_width);
return 0;
}
// ****************************************
//*THIS IS THE FUNCTION FOR GETTING THE
//*RECTANGLE LENGTH
//*****************************************
double getLength(double)
{
double length;
cout << "Please enter the length of the rectangle: ";
cin >> length;
return length;
}
// ****************************************
//*THIS IS THE FUNCTION FOR GETTING THE
//*RECTANGLE WIDTH
//*****************************************
double getWidth(double)
{
double width;
cout << "Please enter the width of the rectancle: ";
cin >> width;
return width;
}
// ****************************************
//* THIS FUNCTION TAKES THE RECTANGLE WIDTH
//* AND LENGTH AND MULTIPLIES THEM TO GIVE
//* US THE RECTANGLES AREA.
//*****************************************
double getArea(double num1, double num2)
{
return num1*num2;
}
// ****************************************
//* THIS FUNCTION ACCEPTS THE RECTANGLES
//* LENGTH, WIDTH, AND AREA AND DIPLAYS
//* THEM ON THE SCREEN.
//*****************************************
double displayData(double rect_length, double rect_width, double rect_area)
{
cout << "The length of the rectangle is " << rect_length << ". " << endl;
cout << "The width of the rectangle is " << rect_width << ". " << endl;
cout << "The area of the rectangle is " << rect_area << ". " << endl;
}
It saying "error C4716: 'displayData' : must return a value" when i try to compile it. What am i doing wrong??
Any help, or tips is greatly appreciated. Thank you!
Either declare the displayData Function as void like:
1 2 3 4 5 6 7 8
void displayData(double rect_length, double rect_width, double rect_area)
{
cout << "The length of the rectangle is " << rect_length << ". " << endl;
cout << "The width of the rectangle is " << rect_width << ". " << endl;
cout << "The area of the rectangle is " << rect_area << ". " << endl;
}
or return a double.
1 2 3 4 5 6 7 8
double displayData(double rect_length, double rect_width, double rect_area)
{
cout << "The length of the rectangle is " << rect_length << ". " << endl;
cout << "The width of the rectangle is " << rect_width << ". " << endl;
cout << "The area of the rectangle is " << rect_area << ". " << endl;
return 0.0
}
void displayData(double rect_length, double rect_width, double rect_area)
{
cout << "The length of the rectangle is " << rect_length << ". " << endl;
cout << "The width of the rectangle is " << rect_width << ". " << endl;
cout << "The area of the rectangle is " << rect_area << ". " << endl;
}
and i changed it to return a double as well.
1 2 3 4 5 6 7 8 9
double displayData(double rect_length, double rect_width, double rect_area)
{
cout << "The length of the rectangle is " << rect_length << ". " << endl;
cout << "The width of the rectangle is " << rect_width << ". " << endl;
cout << "The area of the rectangle is " << rect_area << ". " << endl;
return 0.0
}
and the error for both of them was:
" 1>main.obj : error LNK2019: unresolved external symbol "double __cdecl getWidth(void)" (?getWidth@@YANXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "double __cdecl getLength(void)" (?getLength@@YANXZ) referenced in function _main
Well, the functions called on main is getWidth() and getLength(). However, you've only defined the functions getLength(double) and getWidth(double). Remove those doubles and it should be fine.