用于 C++ 中用户输入的 2D 数组

2D array for user input in c++

本文关键字:2D 数组 输入 用户 C++ 用于      更新时间:2023-10-16

我是 c++ 初学者,我想问一个简单的 2D 数组代码:我想创建这样的数据类型

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
int players=5;
int age[10]={0};
int basket_count[10][5]={0};
string name[10];
main()
{
int n=1;
int i=0;
int j=0;
int k=0;
int l=0;
while (n<=players)
{
cout<<n<<" Player is"<<endl;
cin>>name[i];
cin>>age[j];
while (k<players&&l<5)
{
    cin>>basket_count[k][l];
    k++;
    l++;
}
n++;
i++;
j++;
}
for (int i=0;i<players;i++)
{
for (int j=0;j<5;j++)
    cout<<basket_count[i][j];
}
return 0;
}

如果有人可以纠正我的代码,我将非常感谢您!!

这应该是:

while (l<5)   
{    
    cin>>basket_count[n][l];    
    l++;    
}

您想填充数组的第 n 行,因此不需要使用其他计数器。此外,当您填充行时,n 不会改变。

这一点,并且在代码

中使用一个字母的变量并不是最好的主意,代码很难理解。使用编码标准(包括缩进(,将数据组织在结构中,这将对您有很大帮助。

我只是更改您的代码。您的代码现在可以了。让我们试试这个:

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <windows.h>
using namespace std;
int players=5;
int age[10]= {0};
int basket_count[10][5]= {0};
string name[10];
main()
{
    for (int n=0; n<players; n++)
    {
        cout<<n+1<<" Player is"<<endl;
        cin>>name[n];
        cin>>age[n];
        for (int i=0; i<5; i++)
        {
            cin>>basket_count[n][i];
        }
    }
    for (int i=0; i<players; i++)
    {
        for (int j=0; j<5; j++)
            cout<<basket_count[i][j];
    }
    return 0;
}
好的,

你的代码中有一些奇怪的事情。

  1. 你有很多 int 变量。现在,这不一定是坏事,但您使用它们的方式并不是很好。首先,您应该使用更好的名称,以便我们实际了解变量的含义。我倾向于使用 pos 而不是 i,除非我在循环中迭代,这样我知道变量是数组中的位置。在你的第一个循环中,你的j不做任何事情,所以它可以被废弃,因为你试图访问的名称和年龄的元素位于同一位置。

  2. 在第二个 while 循环中,您在递增basket_count多维数组时遇到问题。如果你要浏览代码并记下数字,你会看到你将值保存到 [1][1]、[2][2] 等。如果你只想写出与它匹配的玩家对应的五个变量,那么你应该使用 basket_count[i][k] 或basket_count[i][l],因为 k 和 l 是相同的。您也可以改用 for 循环来更轻松地限制循环的边界。它应该看起来更像最后嵌套的 for 循环。

  3. 你的主函数应该有一个 int 类型,所以它正确地返回 0。现在可能没什么大不了的,但可能会晚得多。

  4. 只是一些提示。学习如何更好地缩进;它使阅读代码变得更加容易,并帮助它保持井井有条。我注意到的一件小事是,在您的嵌套 for 循环中,您只向外部 for loop 提供了花括号。您的代码应该可以正常工作,但是由于您是初学者,为了安全起见,最好在所有循环和if语句上都加上大括号。

struct DATA
{
      string name;
      int age;
      int basket_count[5];
} data[10];

使用此结构存储数据:

cin>>data[n].name;
cin>>data[n].age;
while (l<5)
{
    cin>>data[n].basket_count[l];
    l++;
}

这是一种创建、填充、打印和删除简单 2D 数组的方法......

#include<iostream>
#include<exception>
using namespace std;
int main() {
    int rows{ 0 }, cols{0};
    int** data;
    cout << "Enter number of rows: ";
    cin >> rows;
    cout << "Enter number of columns: ";
    cin >> cols;
    try {
        data = new int*[rows];
        // Allocate each row
        for (int i = 0; i < rows; i++) {
            data[i] = new int[cols];
        }
        // Fill 2D array with data
        int temp{ 0 };
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cout << "Enter value for data[" << i << "][" << j << "]: ";
                cin >> temp;
                data[i][j] = temp;
            }
        }
        // Print array
        cout << "Your array is..." << endl;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                cout << data[i][j] << " ";
            }
            cout << endl;
        }
        // Delete array
        for (int i = 0; i < rows; i++) {
            delete[] data[i];
        }
        delete[] data;
        data = nullptr;
    }
    catch (const std::exception &e) {
        cerr << e.what();
    }
    // system("pause");
    return 0;
}