用于 C++ 中用户输入的 2D 数组
2D array for user input in c++
我是 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;
}
你的代码中有一些奇怪的事情。
你有很多 int 变量。现在,这不一定是坏事,但您使用它们的方式并不是很好。首先,您应该使用更好的名称,以便我们实际了解变量的含义。我倾向于使用 pos 而不是 i,除非我在循环中迭代,这样我知道变量是数组中的位置。在你的第一个循环中,你的j不做任何事情,所以它可以被废弃,因为你试图访问的名称和年龄的元素位于同一位置。
在第二个 while 循环中,您在递增basket_count多维数组时遇到问题。如果你要浏览代码并记下数字,你会看到你将值保存到 [1][1]、[2][2] 等。如果你只想写出与它匹配的玩家对应的五个变量,那么你应该使用 basket_count[i][k] 或basket_count[i][l],因为 k 和 l 是相同的。您也可以改用 for 循环来更轻松地限制循环的边界。它应该看起来更像最后嵌套的 for 循环。
你的主函数应该有一个 int 类型,所以它正确地返回 0。现在可能没什么大不了的,但可能会晚得多。
只是一些提示。学习如何更好地缩进;它使阅读代码变得更加容易,并帮助它保持井井有条。我注意到的一件小事是,在您的嵌套 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;
}
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何创建一个所有行大小不同的 2D 数组,并且用户将指定每行将有多少个元素?
- 使用指针算法修改函数中的 2D 数组
- 在C++中初始化和打印 2D 数组
- 有没有办法使用输入的维度初始化 2d 数组中的所有项目而不循环C++?
- C++ 2D 数组写入多个位置
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 如何从initializer_list初始化 2D 数组
- 交换函数不是在 C++ 中交换 2D 数组的元素