c++ - 在 C++ 中用于用户输入的二维数组

标签 c++ arrays

我是c++初学者,想请教一个二维数组的简单代码: I want to creat a data type like this

#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 也不会改变。

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

关于c++ - 在 C++ 中用于用户输入的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327029/

相关文章:

c++ - 无法访问集合中的集合 | C++

c++ - _strrev 运行方式

c++ - 实现一个简单的 COM 对象

c - C 中的 typedef、数组和指针

python - 排除基于 0 的索引中的最后一个元素

c++ - 使用ProtocolBuffer时,是否可以为生成的C++代码指定输出导入?

c++ - Visual Studio sizeof 非法操作数

javascript - 如何使用javascript函数在数组数组中搜索字符串

c - 将数组传递给方法时使用指针更改数组

python - numpy.unravel_index 不返回预期的行索引