【问题标题】:How to make an array come out as a word not numbers如何使数组以单词而不是数字的形式出现
【发布时间】: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 &lt;&lt; "A 2m " &lt;&lt; waste 没有意义。 waste 是一个字符串数组,而不是单个字符串。您到底希望这条线打印什么?实际上,它会将waste 的地址打印为十六进制数字。
  • 我希望它在废品中打印选定的数组,所以就像如果我输入一般废品,它会打印“A 2m(一般废品)”

标签: c++ arrays char


【解决方案1】:

您的代码中有几个问题。让我们从您在数组中分配 11 个字符串但您读取第 12 个的事实开始。从这一刻开始,一切都可能发生,这是未定义的行为。

接下来,只有在volume &gt; 31 肯定是错误时才返回 0。

最后,你输出二维数组而不是数组的元素:

cout << "A 2m " << waste << endl;

你是说

cout << "A 2m " << waste[0] << endl;

或者类似的?

【讨论】:

  • 1) 我想让用户输入一个数字,他输入的数字代表每个数组;所以我需要帮助来显示所选数组。 2)我做了一个多维区域,第一个[11] = 11个词/分组词。 [50] 表示最大字符数限制。
  • @LectroFiy 如果你想输入一个数字,那么只需输入一个数字。 cin &gt;&gt; i;
  • @LectroFiy 您正在以一种非常容易出错的方式重新发明轮子。请改用std::string
  • 我希望将输入的数字链接到分配的数组;并使用 std::string 代替 char?
  • @LectroFiy 你可以输入一个数字,然后用这个数字作为索引。 int k; cin &gt;&gt; k; // use waste[k];
猜你喜欢
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多