我是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/