issues with getColor function. need help

Raul Perez 21 Reputation points
2021-03-28T19:37:05.917+00:00

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]: /api/attachments/82184-image.png?platform=QnA

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,622 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,661 Reputation points
    2021-03-28T20:04:09.5+00:00

    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).

    No comments

1 additional answer

Sort by: Most helpful
  1. WayneAKing 4,246 Reputation points
    2021-03-28T20:47:28.58+00:00

    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
    No comments