我想让用户选择一个他们想要保留的数字并放入数组中。并将它们放入单独的索引中

I want to have a user select a number that they want to keep and put in an array. And have them put into separate index's

本文关键字:数组 单独 索引 数字 保留 选择 用户 他们 一个      更新时间:2024-09-29

到目前为止,我已经询问用户他们想要持有什么骰子,并将这些骰子放入一个数组中。但当我把它放在数组中时,我希望这些数字保持不变。但在下一轮中,我希望能够用其他数字添加到数组中,而不会改变其他数字。

cin >> Hold;
switch (Hold)
{
case 1: DieHolder[0] = die1;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 2: DieHolder[1] = die2;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 3: DieHolder[2] = die3;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 4: DieHolder[3] = die4;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 5: DieHolder[4] = die5;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
}

我有它,所以数字被放在一个特定的位置,但如果它不改变以前的数字,似乎就无法从下一个转弯开始添加。

然后我把它显示在屏幕上。

for (int i = 0; i < 5; i++)
{
if (DieHolder[i] >= 1)
{
cout << i << ": " << DieHolder[i] << "n";
}
}

我不知道如何保存旧号码。

您面临的问题主要是在for循环和switch语句中。每次运行它时,都会覆盖同一索引上的元素。

有很多方法可以做到这一点,但我建议使用向量,因为它会为您分配内存。对于一个不断增长的常规c样式数组(正如您正在使用的(,您必须自己调整保留内存的大小,以便它能够安全地增长。

这将是一种更安全的方法,也是一种更动态的问题解决方案。

  1. 创建一个std::vector< std::vector<> >类型,使其在二维中重新表示。(把它想象成一个网格,沿着Y是用户选择的行。沿着X是放置在那里的模具(
  • 不建议使用"使用std;"由于潜力定义冲突,但对于学习目的来说,这是可以的。并且由于不必在CCD_ 3 之前写入CCD_

    /* ... Outside Loop Body ... */  
    using std; 
    // This will initialize 5 rows of vectors, which will be where choices stack   
    vector<vector<int>> DieHolder(5, vector<int>{});  
    

循环体内:

  1. 我的假设是,开关是将die1、die2等放置在特定的"行"中

  2. 如下使用at()就是访问某一行。

  3. 访问给定行后,使用emplace_back()在属于该行的末尾列上构造一个新元素。

    /* ... Loop Body ... */ 
    cin >> Hold;
    switch (Hold)
    {
    // emplace_back() Will NOT overwrite, it will stack choices.
    case 1: DieHolder.at(0).emplace_back(die1);
    break;
    case 2: DieHolder.at(1).emplace_back(die2);
    break;
    case 3: DieHolder.at(2).emplace_back(die3);
    break;
    case 4: DieHolder.at(3).emplace_back(die4);
    break;
    case 5: DieHolder.at(4).emplace_back(die5);
    break;
    }
    cout << "If you are done choosing dice use the 0 key to end." << endl;
    /* ... */
    

然后使用嵌套的基于范围的循环,您可以打印出每个元素的内容,如

/* ... */
for (auto & elements_row : DieHolder ){
for(auto & elements_column : elements_row){
cout << "Row: " << elements_row << endl;
if (elements_column >= 1){
cout << "Column: " << elements_column << endl;
}
}
}
/* ... */

尝试使用二维数组。例如,假设die1定义为:

int *die1;
int d1loc = 0;

die1int * (int pointer),因此它指向int的位置。d1loc表示哪个int (dice number)die1处于开启状态。

这是您的程序重写为使用二维数组。

int *die1, die2, die3, die4, die5;
int dloc[5] = {0};
cin >> Hold;
switch (Hold)
{
case 1: DieHolder[0][dloc[0]++] = die1;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 2: DieHolder[1][dloc[1]++] = die2;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 3: DieHolder[2][dloc[2]++] = die3;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 4: DieHolder[3][dloc[3]++] = die4;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
case 5: DieHolder[4][dloc[4]++] = die5;
cout << "If you are done choosing dice use the 0 key to end." << endl;
break;
}
for (int i = 1; i < 6; i++)
for (int j = 0; j < dloc[i][j]; j++)
cout << i << ": " << DieHolder[i][j] << "n";

你自己试试这个,如果它不起作用,请在评论中告诉我