question

RaulPerez-9530 avatar image
0 Votes"
RaulPerez-9530 asked WayneAKing-0228 answered

issues with getColor function. need help

include <iostream> #include <string> using namespace std; // Function Prototypes double getLength(); double getWidth(); int getColor(); double getArea(double, double); void displayData(double, double, double, string); int main() { double length; // The rectangle's length double width; // The rectangle's width double area; // The rectangle's area string color; // The rectangle's color // Get the rectangle's length. length = getLength(); // Get the rectangle's width. width = getWidth(); // Get the rectangle's color color = getColor(); // Get the rectangle's area. area = getArea(length, width); // Display the rectangle's data. displayData(length, width, area, color); return 0; } // // getLength // double getLength() { double length; // rectangle's length and return value as a double. // Get the length cout << "Enter the length: "; cin >> length; return length; } // // getWidth // double getWidth() { double width; // rectangle's width and return valus as a double. // Get the width cout << "Enter the width: "; cin >> width; return width; } // // getColor // int getColor() { int color; // rectangle's color. // Get the color cout << "Choose your color by typing the number next to your color \n 1 Blue \n 2 Red \n 3 Green \n 4 Yellow \n 5 Pink \n 6 Orange "; string blue = "Blue"; string red = "Red"; string green = "Green"; string yellow = "Yellow"; string pink = "Pink"; string orange = "Orange"; cin >> color; if (color == 1) { cout << blue; color = 1; }; if (color == 2) { cout << red; color = 2; }; if (color == 3) { cout << green; color = 3; }; if (color == 4) { cout << yellow; color = 4; }; if (color == 5) { cout << pink; color = 5; }; if (color == 6) { cout << orange; color = 6; }; return color; } // // getArea // double getArea(double length, double width) { return length width; } // //displayData // void displayData(double length, double width, double area, string color) { cout << "\n Rectangle Data \n" << "------------- \n" << "Length: " << length << endl << "Width: " << width << endl << "Color: "<< color << endl << "Area: " << area << endl; } no clue if my code is correct... doesnt appear to be. can you help ![82184-image.png][1] [1]: /answers/storage/attachments/82184-image.png

c++
image.png (27.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

You can replace ‘color = getColor()’ with ‘color = to_string(getColor())’, but probably you want to return a string instead of number from getColor().

Or use int instead of string for color variables (in several places).

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WayneAKing-0228 avatar image
0 Votes"
WayneAKing-0228 answered

You may be confusing yourself because you are using
the same name for both the numeric (int) color value
and the alpha (string) name of the color. If you think
about the code you have written such as this:

 cin >> color; 
 if (color == 1) 
 { 
     cout << blue; 
     color = 1; 
 };

it should be apparent that something isn't quite right.

If color is equal to 1, what sense does it make to set
it to 1 - the value that it already holds? As Viorel_
suggested, you are not keeping distinct the places
that you want to use a number to represent the color
and the places where you want the name of the color.

  • Wayne

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.