C++ 如何使数组显示为单词而不是数字
我正在建立一个垃圾箱大小计算器,我需要的类型垃圾箱也出来作为一个字,而不是数字 我尝试为每个bin名称创建单独的字符串,但没有成功C++ 如何使数组显示为单词而不是数字,c++,arrays,char,C++,Arrays,Char,我正在建立一个垃圾箱大小计算器,我需要的类型垃圾箱也出来作为一个字,而不是数字 我尝试为每个bin名称创建单独的字符串,但没有成功 #include<iostream> #include <string> using namespace std; int main() { int i; cout << "Welcome to Jasons bin size measurer!\n\nPlease answer the following
#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;
}
}
#包括
#包括
使用名称空间std;
int main(){
int i;
cout您的代码中有几个问题。让我们从您在数组中分配了11个字符串开始,但您读取了第12个字符串。从现在开始,一切都可能发生,这是未定义的行为
接下来,仅当volume>31
时才返回0,这肯定是一个错误
最后,输出二维数组而不是数组的元素:
cout << "A 2m " << waste << endl;
cout另外,我还想获得一些关于如何优化代码的帮助您显示的代码不会在“需求”一词之后打印任何内容-既不是bin名称,也不是数字。您实际运行的代码,即产生不希望的输出的代码,必须与显示的代码不同。@IgorTandetnik当我运行它时,结果与我描述的一样,我正在使用visual studio c++我是否希望它在WAST中打印选定的数组,就像我输入General waste时,它会打印“a”(一般浪费)“1)我希望用户输入一个数字,他输入的数字代表每个数组;因此我需要显示所选数组的帮助。2)我制作了一个多维区域,首先[11]=11个单词/分组单词。和[50]表示最大字符限制。@Electrofy如果您想输入一个数字,那么只需输入一个数字。cin>>i;
@Electrofy您正在以一种非常容易出错的方式重新发明一个轮子。请改用std::string
。我希望输入的数字链接到指定的数组;并使用std::string而不是char?@Electrofy您可以吗输入一个数字,然后将该数字用作索引。int k;cin>>k;//使用废物[k];
cout << "A 2m " << waste[0] << endl;