Help requested for programming functions.

I have a very basic understanding of how to code functions. I understand that you must prototype the functions before using them. I am attempting to write a simple program utilizes four functions.

1) getLength - This function should ask the user to enter a rectangle's length and then return it has a double.
2) getWidth - This function should ask the user to enter a rectangle's width and then return it has a double.
3) getArea - should accept the length and width as arguments and return the rectangle's area. (area=length*width)
4) displayData - should accept the length, width, and area as argument and display them in an appropriate message on the screen.

I have attached what coding I have done so far. If someone would so kind as to code the length and area, I believe I can code the rest from their lead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
#include<iomanip>
#include<cmath>

double getLength();
double getWidth();
double getArea();
void displayData(double,double,double);

main()
{
    double length, // The Rectangle's length
           width,  // The Rectangle's Width
           area;   // The Rectangle's 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;
}
here is somthing that could help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <limits>
using namespace std;
#define CLRJUNK cin.ignore();
#define PROMPT cout << "<Press ENTER KEY to continue>";
#define PAUSE cin.ignore(numeric_limits<streamsize>::max(), '\n' );
void getData(int & theHeight, int & theLength, int & theWidth)
{

	cout << "Enter the height: ";
	cin >> theHeight;

    cout << "Enter the width: ";	
	cin >> theWidth;


	cout << "Enter the length: ";
	cin >> theLength;
}

void calculateVolume(int &theHeight, int &theLength, int &theWidth,int &theVolume)
{
	
theVolume =  theHeight  *  theLength  *  theWidth ;

}

void displayOutput( int &theVolume)
{
if ((theVolume < 100))//&&(theVolume>0))
cout << "Small"<<endl;
else if ((theVolume >= 100) && (theVolume <= 500))
cout << "Medium"<<endl;
else if (theVolume > 500)
cout << "Large"<<endl;
else
cout << "Umm... WTF Dude."<<endl;
}

int main ()
{
int theHeight=0;
int theWidth=0;
int theLength=0;
int theVolume=0;
int amount=0;

	cout << "How many time you want to run this: ";
	cin >> amount;

for (int i = 0 ; i < amount;i++)
{
getData(theHeight, theWidth, theLength);
 calculateVolume(theHeight, theWidth, theLength,theVolume);

displayOutput(theVolume);

}

PROMPT;
PAUSE;
return 0;
}
Last edited on
interesting way of programming. As I am mostly new this style is a little hard to follow without more detail comments. possible to use what I have coded format?
closed account (zb0S216C)
A definition would take this form:

< Return-Type > < Identifier > ( < Parameter-List > )
{
< Body >
}

A real-world example:

1
2
3
4
5
6
7
8
9
double getLength( )
{
    // Get the user's input:
    double Input( 0.0 );
    std::cin >> Input;

    // Return the user's input:
    return( Input );
}

Here's what's going on:

1) Define what's called the function header. This consists of the function's return-type, name, and parameter list.

2) After, define the function's body. This is where the function's inner-workings reside.
3) Return from the function. This is achieved with the return statement. A function must have a return statement, except for a function that returns void.

I hope this helps.

References:
Functions (I): http://www.cplusplus.com/doc/tutorial/functions/
Functions (II): http://www.cplusplus.com/doc/tutorial/functions2/
Functions: http://cpp-tutorial.cpp4u.com/structures_functions.html


Wazzak
Last edited on
I am very grateful for the help. I coded what I believe is a good basic program. I am having issues when I try to complie it.
line 29 states "no match for 'operator<<' in 'std::cout << displayData()' "
line 32 and 40 states "expected primary-expression before "double"" and " expected `;' before "double""
line 49 states "a function-definition is not allowed here before '{' token" and "expected `;' before "double""

Any help in resoving these issues will be greatly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes 
double getLength();
double getWidth();
double getArea();
void displayData();

main()
{
    double length,  // The rectangle's length
               width,   // The rectangle's width
               area;    // The rectangle's area
    
    cout << getLength();
    cout << getWidth();
    cout << getArea();
    cout << displayData(); //Line 29
           
      // Get the rectangle's length.
    double getLength() //Line 32
    {
        cout << "Please enter the rectangle's length: ";
        cin >> length;
        return (length);
     }
    
    // Get the rectangle's width.
    double getWidth() //Line 40
    {
        cout << "\nPlease enter the rectangle's width: ";
        cin >> width;
        return (width);
    }
    
    // Get the rectangle's area.
    double getArea(double length, double width)
    { //line 49
        area = (length * width);
        return (area);
    }
    
    // Display the rectangle's data.
      void displayData(double length, double width, double area);
    {
        cout << "The rectangle's length is: " << length << endl;
        cout << "The rectangle's width is: " << width << endl;
        cout << "The rectangle's area is: " << area << endl;
    }
    
    system("pause");
    return 0;
}
1) The reason you're getting the cout error for that particular function is because it returns nothing. It has a void return type, which you cannot cout.

2) All of your function definitions should be outside (i.e. below) main().

3) The function definitions for getArea and displayData don't match their prototypes. The prototypes have no parameters and the definitions do have parameters.

Subzero, your example may work but, as this looks like a school project, it's important to meet the specification:
I am attempting to write a simple program utilizes four functions.


Also, the spec mentions that the functions need to return values, so pass-by-reference isn't a good option.

Here's how I'd do length. The rest you should be able to work out from it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

double GetLength();

int main()
{
   double length = 0;
   length = GetLength();
   return 0;
}

double GetLength()
{
   double input;
   cout << "Please enter length: ";
   cin >> input;
   // You may want to add validation here to check input is valid
   return input;
}
Last edited on
Function definitions need to go after the closing brace in main.

displayData doesn't return anything. So of course, cout doesn't know what you want done with the nothing you're returning. displayData also takes parameters. This is not reflected in the function prototype before main, and it should be.

When you call getArea() and displayData() you don't provide any arguments to them.

When you call getLength() and getWidth() you don't assign the return value to anything. You need to do so, so you can then provide those values to getArea().
I copied and pasted iHutch105's example (i hope you don't mind) and attempted to build my program around it. I have linker error message that does not make sense to me. PLease let me know where my mistake is at and a possible way to fix it.

error message:
[Linker error] undefined reference to `getArea()'
[Linker error] undefined reference to `displayData()'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

double getLength();
double getWidth();
double getArea();
void displayData();

int main()
{
   double length = 0;
   double wide = 0;
   double area = 0;
   length = getLength();
   wide = getWidth();
   area = getArea();
   displayData();
   
   system("pause");
   return 0;
}

double getLength()
{
   double length;
   cout << "Please enter length: ";
   cin >> length;
   // You may want to add validation here to check input is valid
   return length;
} 

double getWidth()
{
   double width;
   cout << "Please enter width: ";
   cin >> width;
   // You may want to add validation here to check input is valid
   return width;
} 

double getArea(double length, double width)
{
  double area;
  area = (length * width);
  return area;
} 
void displayData(double length, double width, double area) 
{
    cout << "length: " << length << endl;
    cout << "width: " << width << endl;
    cout << "area: " << area << endl;
}
Line 6 and 7 should be:
1
2
double getArea(double length, double width);
double displayData(double length, double width, double area);


Lines 16 and 17 should be:
1
2
area = getArea(length, wide);
displayData(length, wide, area);


The prototypes and definitions should look identical for the return type, function name, and parameter list. Then your use of the function should supply the proper number of parameters for it to work properly.
Last edited on by closed account z6A9GNh0
Thank you to everyone for the help.
Topic archived. No new replies allowed.