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;
}
|