如何解决什么是正确的功能块?
以下代码用于矩形分配。但不确定我的获取颜色是否正确输入。想看看是否有人能帮我解决这个问题。
#include <iostream>
#include <string>
using namespace std;
// Function Prototypes
double getLength();
double getWidth();
int getColor();
double getArea(double,double);
void displayData(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;
}
我不知道这个平方是什么?方法。我在 getColor 函数中做错了什么吗??
解决方法
您的 getColor
方法返回一个 int
,您将它分配给一个 std::string
。您应该返回您向用户显示的字符串(因此,变量 blue
、pink
、red
等)本身就是 std::string
而不是 color
,属于 int
类型。
因此,编译器正在获取您返回的 int
并使用它来初始化 std::string
。如果您调试并查看内容,您会看到字符串的内容与您输入的值相对应。例如,在 Blue (1) 的情况下,它试图向您显示 ASCII 字符 1。如果您输入颜色选择“65”,它将显示颜色“A”,它对应于 ASCII 65(顺便说一句,您应该清理输入,以便用户无法输入无效值)。至于没有警告,我想它只是属于其中一个构造函数的一些允许的类型转换。
顺便说一句,在 getColor
中,我将创建一个包含颜色而不是 N 个变量的字符串数组。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。