Function problems

So I have a project where I have to do the following:

The program calls the following functions, which have not been written:

•getLength –This function should ask the user to enter the rectangle’s length then return that value as a double.

•getWidth –This function should ask the user to enter the rectangle’s width then return that value as a double.

•getColor –This function should ask the user to enter the rectangle’s color then return that value as a string.You will provide the user with a menu of colors to select from and return that valueas an integer.

•getArea –This function should accept the rectangle’s length and width as arguments and return the rectangle’s area. The area is calculated by multiplying the length by the width.

•displayData –This function should accept the rectangle’s length, width, color, and area as arguments and display them in an appropriate message on the screen.

My issue lies on line 127, where visual studio gives me error messages for my displayData function and I'm not sure why. any help?

also, is there anything else I'm doing incorrectly in my code that isn't correct for my assignment? thank you in advance

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  //------------------------------------------------------
//                    AreaRectangle.cpp
//						Lucas Brooks
//						COSC-1436.256
//						Lab 4
// ------------------------------------------------------

#include <iostream>
using namespace std;

// Write the prototypes for the getLength,
// getWidth, getArea, and displayData
// functions here.

double length;    // The rectangle's length 
double width;    // The rectangle's width          
double area;     // The rectangle's area
int color;       // The Rectangle's color 

double getLength();
double getWidth();
double getArea(double, double);
double displayData(double, double, double);
int getColor();



int main() 
{
	

	// Get the rectangle's length.   
	cout << "What is the length of the rectangle?" << endl; 
	length = getLength();

	// Get the rectangle's width.
	cout << "What is the width of the rectangle?" << endl;
	width = getWidth();

	// Get the rectangle's color
	color = getColor();
	cout << "The color of the rectangle is " << color << endl;

	// Get the rectangle's area.
	area = getArea(length, width);
	cout << "The area of the rectangle is " << area << endl;

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

double getLength()
{
	double length;

	cin >> length;
	return length;
}

double getWidth()
{
	double width;

	cin >> width;
	return width;
}

int getColor()
{
	int color;

	cout << "What is the color of the Rectangle?" << endl;
	cout << "/n/n";

	cout << "Please input a number." << endl;
	cout << "1. Red/n 2. Blue/n 3. Green/n 4. Orange/n 5. Purple/n";

	cin >> color;

	if (color == 1)
	{
		cout << "The color is red.";
		}

	else if (color == 2)
	{
		cout << "The color is blue.";
	}

	else if (color == 3)
	{
		cout << "The color is green.";
	}

	else if (color == 4)
	{
		cout << "The color is orange.";
	}

	else if (color == 5)
	{
		cout << "The color is purple.";
	}

	else
	{
		cout << "Please enter a valid number." << endl;
	}

	return color;
}

double getArea(double length, double width)
{

	double area;

	area = length * width;
	return area;
}

double displayData(double length, double width, double area)
{
	double length, width, area;;

	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;
	
	return;
}
luckylukebrooks,

You are using the line using namespace std;. That is alright you will figure it out the hard way.

Avoid using global variable unless they start with "const" or "constexpr" as these type of variables can not be changed.

On line 125 you define the function and the variables that are passed to the function. On line 127 you are redefining these same variables.

Through out your code you define variables all of which should be initialized when defines. From C++11 on the empty {}s make it very easy. double length{};. The empty{}s allows the compiler to set the variables value to the type of (0)zero that matches the variables type.

In the "getColor" function I would consider a switch. If you have not reached that yet no worries.

If statements or switch either way I would have it set a "std::string" variable to the colour name and just have 1 "cout" statement.

Another alternative is to define an array of strings and use the number you input to get the proper colour from the array.
1
2
3
std::string colours[]{ "", "Red", "Blue", "Green", "Orange", "Purple" };

std::cout << "\n Your colour is " << colours[color] <<'\n';


For now that is what I see.

Andy
1. the global variables you say i used are the ones in lines 15-18 right? and i should get rid of them?
@ Handy Andy also my displayData function still says length, width, and area are uninitialized when i try to cout
Hello luckylukebrooks,

Yes, but just move them to inside "main".

When I got the program to run I get this:

What is the length of the rectangle? 5
What is the width of the rectangle? 4
What is the color of the Rectangle?


Please input a number.
 1. Red
 2. Blue
 3. Green
 4. Orange
 5. Purple
  Enter number: 3
The color of the rectangle is Green
The area of the rectangle is 20
The length of the rectangle is 5
The width of the rectangle is 4
The area of the rectangle is 20


The "std::string" array of colour names I mentioned I put that in "main" and not the function. The "getColor" function should just do 1 thing display the menu and get the input then return the input. All those if/else if statements are not needed. Plus you are double printing the colour name and then back in "main" the number. Not the best output.

It would help if you break up the output a bit to make it easier to read. Then it might be easier to notice that you are printing the area of the rectangle twice. Once should be enough.

Another change I made:
1
2
3
4
5
6
7
8
9
double getArea(double length, double width)
{
    return length * width;

    //double area;

    //area = length * width;
    //return area;
}

You do not need all that extra work just to return "length * width".

"displayData" should be a "void" function not one that returns a double espceially since you do not return anything.

in "getColor" I did the menu like this:
1
2
3
4
5
6
7
8
9
10
cout << "\n\nPlease input a number.\n";
cout << 
    " 1. Red\n"
    " 2. Blue\n"
    " 3. Green\n"
    " 4. Orange\n"
    " 5. Purple\n"
    "  Enter number: ";

cin >> color;

You seem to have a problem using the forward slash when you need to be using the backslash.

Even though each looks like an individual string it is considered 1 big string. The other part that works for you is the "cin" will flush the buffer of any output before any input is taken.

Last note: try to use the newline character (\n) instead of the "endl" function.

Andy
Hello luckylukebrooks,


also my displayData function still says length, width, and area are uninitialized when i try to cout


I did not get that error, but I did remove line 127 before I compiled the program.

Andy
@ Handy Andy

I messed around a bit and the global variables were screwing things up, and this code works

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <iomanip>
using namespace std;

// Write the prototypes for the getLength,
// getWidth, getArea, and displayData
// functions here.


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



int main() 
{
	
	double length;    // The rectangle's length 
	double width;    // The rectangle's width          
	double area;     // The rectangle's area
	int color;       // The Rectangle's color 

	// Get the rectangle's length.   
	cout << " What is the length of the rectangle?" << endl; 
	length = getLength();

	// Get the rectangle's width.
	cout << " What is the width of the rectangle?" << endl;
	width = getWidth();

	// Get the rectangle's color
	color = getColor();
		cout << "\n";		

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

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

double getLength()
{
	double length;

	cin >> length;
	return length;
}

double getWidth()
{
	double width;

	cin >> width;
	return width;
}

int getColor()
{
	int color;

	cout << "What is the color of the Rectangle?" << endl;

	cout << "Please input a number. " << endl;
	cout << "1. Red\n";
	cout << "2. Blue\n";
	cout << "3. Green\n";
	cout << "4. Orange\n";
	cout << "5. Purple\n";

		cout << "\n\n";
	cin >> color;
		cout << "\n";

	if (color == 1)
	{
		cout << "The rectangle's color is red.\n";
		}

	else if (color == 2)
	{
		cout << "The rectangle's color is blue.\n";
	}

	else if (color == 3)
	{
		cout << "The color rectangle's is green.\n";
	}

	else if (color == 4)
	{
		cout << "The rectangle's color is orange.\n";
	}

	else if (color == 5)
	{
		cout << "The rectangle's color is purple.\n";
	}

	else
	{
		cout << "Please enter a valid number." << endl;
	}

	return color;
}

double getArea(double length, double width)
{

	double area;

	area = length * width;
	return area;
}

void displayData(double length, double width, double area)
{

	cout << "The length of the rectangle is " << setprecision(2) << length << endl; 
	cout << "The width of the rectangle is " << setprecision(2) << width << endl;
	cout << "The area of the rectangle is " << setprecision(2) << area << endl;
	
}
Hello luckylukebrooks,

Better, but it does not follow your instructions.

The "displayData" function is lacking the 4th parameter for color and the function is missing a line to print the colour.

This is your revised code I have been working on:
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <string>

using namespace std;  // <--- Best not to use.

// Write the prototypes for the getLength,
// getWidth, getArea, and displayData
// functions here.

double getLength();
double getWidth();
double getArea(const double, const double);
void displayData(const double, const double, const double, const int color);
int getColor();

int main()
{
    double length;   // The rectangle's length 
    double width;    // The rectangle's width          
    double area;     // The rectangle's area
    int color;       // The Rectangle's color 
    

    // Get the rectangle's length.   
    cout << "\n What is the length of the rectangle? ";
    length = getLength();

    // Get the rectangle's width.
    cout << " What is the width of the rectangle? ";
    width = getWidth();

    // Get the rectangle's color
    color = getColor();
 
    // Get the rectangle's area.
    area = getArea(length, width);
    //cout << "\n The area of the rectangle is " << area << '\n';

    // Display the rectangle's data.
    displayData(length, width, area, color);  // <--- This was correct to begin with.   
    //displayData(length, width, area);

    return 0;
}

double getLength()
{
    double length;

    cin >> length;

    return length;
}

double getWidth()
{
    double width;

    cin >> width;

    return width;
}

int getColor()
{
    int color;

    cout << " What is the color of the Rectangle?\n";
    //cout << "/n/n";  // <--- Needs "\" not "/".

    cout << "\n Please input a number.\n";
    cout << 
        "  1. Red\n"
        "  2. Blue\n"
        "  3. Green\n"
        "  4. Orange\n"
        "  5. Purple\n"
        "   Enter number: ";

    cin >> color;

    //if (color == 1)
    //{
    //    cout << "The color is red.";
    //}

    //else if (color == 2)
    //{
    //    cout << "The color is blue.";
    //}

    //else if (color == 3)
    //{
    //    cout << "The color is green.";
    //}

    //else if (color == 4)
    //{
    //    cout << "The color is orange.";
    //}

    //else if (color == 5)
    //{
    //    cout << "The color is purple.";
    //}

    //else
    //{
    //    cout << "Please enter a valid number." << endl;
    //}

    return color;
}

double getArea(const double length, const double width)
{
    return length * width;

    //double area;

    //area = length * width;
    //return area;
}

void displayData(const double length, const double width, const double area, const int color)
{
    const std::string colours[]{ "", "Red", "Blue", "Green", "Orange", "Purple" };

    cout << "\n The color of the rectangle is " << colours[color] << '\n';
    cout << " The length of the rectangle is " << length << '\n';
    cout << " The width of the rectangle is " << width << '\n';
    cout << " The area of the rectangle is " << area << '\n';

    //return;
}


And produces this output:

 What is the length of the rectangle? 5
 What is the width of the rectangle? 4
 What is the color of the Rectangle?

 Please input a number.
  1. Red
  2. Blue
  3. Green
  4. Orange
  5. Purple
   Enter number: 5

 The color of the rectangle is Purple
 The length of the rectangle is 5
 The width of the rectangle is 4
 The area of the rectangle is 20



Andy
@ Handy Andy

1. why did you make them constants?

2. how did you get a fourth argument without an error? every time i tried what you did before it came up as an error
@ Handy Andy

I want to use a switch statement like you mentioned earlier (arrays are too new to me to understand at the moment) but it keeps displaying the number inputted rather than the corresponding color.

also, how can i get this to show up with the getData function?

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
int getColor()
{
	int color;

	cout << "What is the color of the Rectangle?" << endl;

	cout << "Please input a number. " << endl;
	cout << "1. Red\n";
	cout << "2. Blue\n";
	cout << "3. Green\n";
	cout << "4. Orange\n";
	cout << "5. Purple\n";

	cout << "\n\n";
	cin >> color;

			switch (color)
	{
			case '1': cout << "The rectangle's color is red. \n";
					break;
			case '2': cout << "The rectangle's color is blue. \n";
					break;
			case '3': cout << "The rectangle's color is green. \n";
					break;
			case '4': cout << "The rectangle's color is orange. \n";
					break;
			case '5': cout << "The rectangle's color is purple. \n";
					break;
			default: cout << "Please input a valid number.\n";
	}
	return color;
}

double getArea(double length, double width)
{
	double area;

	area = length * width;
	return area;
	
}

void displayData(double length, double width, double area, int color)
{

	cout << "\n The color of the rectangle is " << color << '\n';
	cout << " The length of the rectangle is " << length << '\n';
	cout << " The width of the rectangle is " << width << '\n';
	cout << " The area of the rectangle is " << area << '\n';
}
Hello luckylukebrooks,

I believe you are referring to the last 2 functions. You can think of it as a safety. You can use the variables an their values, but you can not change nor would you want to.

As for question 2. I made the prototype, function call and function definition all match. Order makes no difference as long as they all match. I also like to copy the complete function definition and use it for the prototype. The MSVS ide used the prototype to give a hint when writing the function call. Having the variables names really helps. Also it is a cheep way to make everything match.

Sorry anything else will have to wait until tomorrow morning.

Andy
@ handy andy

thank you for the help this evening
why did you make them constants?

It is pointless to make them constant in the function declarations since it is not part of the interface (i.e., the user of the function doesn't care that you have made your local copy constant). If you want, you can make them constant in the function definitions, although it is not commonly done.
It's sometimes done to 'catch' errors whereby the variable is changed in the code and the coder hasn't noticed it's passed by value and not by ref - so the change isn't reflected in the calling function when it should be. By passing as a const value, this sort of error is caught by the compiler. Of course, there are times when a by-value variable is changed without the change being wanted to be reflected....
Hello luckylukebrooks,

Just so you know in your switch:
1
2
3
4
switch (color)
{
    case '1': cout << "The rectangle's color is red. \n";
        break;

The variable "color" is defined as an int, but the case is looking for a "char". Loose the single quotes and it should work fine, but in the end you really do not need a switch or even if statements.

After looking at:

•getColor –This function should ask the user to enter the rectangle’s color then return that value as a string.
You will provide the user with a menu of colors to select from and return that valueas an integer.

I am not sure how to take line 2. The return part does not make any sense.

In any case I read this as:
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
std::string getColor()
{
    int color;

    cout << " What is the color of the Rectangle?\n";
    //cout << "/n/n";  // <--- Needs "\" not "/".

    cout << "\n Please input a number.\n";
    cout << 
        "  1. Red\n"
        "  2. Blue\n"
        "  3. Green\n"
        "  4. Orange\n"
        "  5. Purple\n"
        "   Enter number: ";

    cin >> color;

    switch (color)
    {
        case 1:  // <--- No quotes.
            return "Red";
        case 2:
            return "Blue";
        default:
            break;
    }

    return "None";
}

You may want to put something in the "default" case. It will take me a few minutes, but I have another idea for this function.

You will also have to change the forward declarations for "getColor" and "displayData" along with the variable in "main".

Andy


Hello luckylukebrooks,

My revised function:
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
std::string getColor()
{
    int color;

    do
    {
        cout << " What is the color of the Rectangle?\n";
        //cout << "/n/n";  // <--- Needs "\" not "/".

        cout << "\n Please input a number.\n";
        cout <<
            "  1. Red\n"
            "  2. Blue\n"
            "  3. Green\n"
            "  4. Orange\n"
            "  5. Purple\n"
            "   Enter number: ";

        cin >> color;

        switch (color)
        {
            case 1:
                return "Red";
            case 2:
                return "Blue";
            default:
                std::cout << "\n     Invalid Input! Must be a number.\n\n";

                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
                break;
        }

    } while (true);

    return "None";  // <--- This keeps the compiler happy, but you should never reach here.
}


Andy
1
2
3
4
getColor –This function should ask the user to enter the rectangle’s color 
then return that value as a string. 
You will provide the user with a menu of colors to select from
 and return that value as an integer.


This requirement is contradictory. Should the function return a string or an int???


Assuming return a string, then consider:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <limits>

double getLength();
double getWidth();
double getArea(double, double);
void displayData(double, double, double, const std::string& color);
std::string getColor();
double getNum(const std::string&);
int getInt(const std::string& prm);

int main()
{
	const auto length {getLength()};
	const auto width {getWidth()};
	const auto color {getColor()};
	const auto area {getArea(length, width)};

	displayData(length, width, area, color);
}

double getLength()
{
	return getNum("What is the length of the rectangle? ");
}

double getWidth()
{
	return getNum("What is the width of the rectangle? ");
}

std::string getColor()
{
	while (true) {
		std::cout << "What is the color of the Rectangle?\n";
		std::cout <<
			"1. Red\n"
			"2. Blue\n"
			"3. Green\n"
			"4. Orange\n"
			"5. Purple\n";

		const auto color {getInt("Please input a number: ")};

		switch (color) {
			case 1: return "red";
			case 2: return "blue";
			case 3: return "green";
			case 4: return "orange";
			case 5: return "purple";
			default: std::cout << "Invalid color number\n";
		}
	}
}

double getArea(double length, double width)
{
	return length * width;
}

void displayData(double length, double width, double area, const std::string& color)
{
	std::cout << "\nThe color of the rectangle is " << color << '\n';
	std::cout << "The length of the rectangle is " << length << '\n';
	std::cout << "The width of the rectangle is " << width << '\n';
	std::cout << "The area of the rectangle is " << area << '\n';
}

double getNum(const std::string& prm)
{
	double d {};

	while ((std::cout << prm) && (!(std::cin >> d) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return d;
}

int getInt(const std::string& prm)
{
	int i {};

	while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
		std::cout << "Not a number\n";
		std::cin.clear();
		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	return i;
}



What is the length of the rectangle? 2q
Not a number
What is the length of the rectangle? 4
What is the width of the rectangle? w3
Not a number
What is the width of the rectangle? 3
What is the color of the Rectangle?
1. Red
2. Blue
3. Green
4. Orange
5. Purple
Please input a number: 6
Invalid color number
What is the color of the Rectangle?
1. Red
2. Blue
3. Green
4. Orange
5. Purple
Please input a number: s
Not a number
Please input a number: 4

The color of the rectangle is orange
The length of the rectangle is 4
The width of the rectangle is 3
The area of the rectangle is 12

Last edited on
@seeplus,

I missed the lack of the space in line 2, but I agree it is contradictory.

When you look at the use of the switch returning a string makes more sense.

@luckylukebrooks,

I revised the "default case to be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
default:
    if (!std::cin)  // <--- To catch any non numeric input.
    {
        std::cout << "\n     Invalid Input! Must be a number.\n\n";

        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    }
    else if (color < 1 || color > 5)  // <--- To catch anything out of range.
    {
        std::cout << "\n     Invalid Choice!. Must be 1 - 5.\n\n";
    }
    break;

This keeps you in the do/while loop until you enter a valid number.

I realize that this may be ahead of what you have learned, but there is no time like the present to learn something new.

The big point here is if you enter any non numeric value "std::cin" will be in a failed state and be unusable the rest of the program until the program is stopped or the problem is corrected.

Andy
Topic archived. No new replies allowed.