【发布时间】:2019-09-30 01:57:57
【问题描述】:
我正在构建一个 bin 大小计算器,我需要 bin 的类型也以单词而不是数字的形式出现
我尝试为每个 bin 名称创建单独的字符串,但没有成功。
#include<iostream>
#include <string>
using namespace std;
int main() {
int i;
cout << "Welcome to Jasons bin size measurer!\n\nPlease answer the following questions: " << endl;
cout << " Please Tell us your waste type " << endl << "By typing the corrosponding number: " << endl;
char waste[11][50] = { " ", "General Waste = 1","Mixed Heavy Waste = 2", "Timber Waste = 3","Green Garden Waste = 4 ","Soil/Dirt = 5","Clean Fill/Hard Fill = 6","Concrete only = 7","Bricks only = 8", "Cardboard/Paper = 9","Metal only = 10" };
for (i = 0; i < 11; i++) {
cout << waste[i] << "\n";
}
cin >> waste[i];
int length = 0;
int width = 0;
int height = 0;
cout << "\nPlease enter the length: ";
cin >> length;
cout << endl << "Please enter the width: ";
cin >> width;
cout << endl << "Please enter the height: ";
cin >> height;
int volume = length * width * height;
cout << endl;
if (volume < 3) {
cout << "A 2m " << waste << endl;
}
if (volume < 4 && volume > 2) {
cout << "\nA 3m bin ";
}
if (volume < 5 && volume > 3) {
cout << "\nA 4m bin will meet your requirements";
}
if (volume < 7 && volume > 4) {
cout << "\nA 6m bin will meet your requirements";
}
if (volume < 9 && volume > 6) {
cout << "\nA 8m bin will meet your requirements";
}
if (volume < 11 && volume > 8) {
cout << "\nA 10m bin will meet your requirements";
}
if (volume < 13 && volume > 10) {
cout << "\nA 12m bin will meet your requirements";
}
if (volume < 22 && volume > 12) {
cout << "\nA 21m bin will meet your requirements";
}
if (volume < 25 && volume > 21) {
cout << "\nA 24m bin will meet your requirements";
}
if (volume < 32 && volume > 24) {
cout << "\nA 31m bin will meet your requirements";
}
if (volume > 31) {
cout << "\nUnfortunatly we do not have that bin size";
return 0;
}
}
我希望输出为x bin will meet your requirements(bin name)
但是它以X bin will meet your requirements 008FFA1C 的形式出现。
【问题讨论】:
-
另外,我想获得一些关于如何优化代码的帮助
-
您显示的代码不会在“要求”一词之后打印任何内容 - 既不是 bin 名称也不是数字。您实际运行的代码(产生不希望的输出)必须与显示的代码不同。
-
@IgorTandetnik 当我运行它时,它就像我描述的那样,我使用的是 Visual Studio C++
-
cout << "A 2m " << waste没有意义。waste是一个字符串数组,而不是单个字符串。您到底希望这条线打印什么?实际上,它会将waste的地址打印为十六进制数字。 -
我希望它在废品中打印选定的数组,所以就像如果我输入一般废品,它会打印“A 2m(一般废品)”