简体   繁体   English

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

[英]2D array for user input in c++

I am a c++ beginner, I want to ask a simple code of 2D array: I want to creat a data type like this 我是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;
}

If there is anybody can correct my code, I will very appreciate you!! 如果有人可以更正我的代码,我将非常感谢您!!

This should be: 应该是:

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

You want to fill the n'th row of your array, so no need in another counter. 您要填充数组的第n行,因此无需其他计数器。 Also the n doesn't change while you fill the row. 同样,当您填充行时,n不会改变。

That, and using one-letter variables all over your code is not the best idea, the code is difficult to understand. 那样的话,在整个代码中使用一个字母的变量不是最好的主意,那么代码很难理解。 Use coding standards( including identations), organize your data in structs, it will help you a lot. 使用编码标准(包括标识),按照结构组织数据,这将对您有很大帮助。

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

Use this struct to store data: 使用此结构存储数据:

cin>>data[n].name;
cin>>data[n].age;

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

Okay you have a few things going on weird in your code. 好的,您的代码中发生了一些奇怪的事情。

  1. You have a lot of int variables. 您有很多int变量。 Now, this isn't necessarily bad, but the way you use them isn't great. 现在,这不一定很糟糕,但是您使用它们的方式并不好。 To begin, you should use better names so we can actually see what the variables mean. 首先,您应该使用更好的名称,以便我们可以实际了解变量的含义。 I tend to use pos instead of i unless I'm iterating in a loop, that way I know that variable is for the position in the array. 除非我在循环中进行迭代,否则我倾向于使用pos而不是i,那样我就知道变量是针对数组中的位置的。 In your first loop, your j doesn't do anything, so it can be scrapped as the element in name and age you're trying to access is at the same location. 在第一个循环中,您的j不执行任何操作,因此可以将其废弃,因为您要访问的名称和年龄元素位于同一位置。

  2. In your second while loop, you have a problem incrementing your basket_count multidimensional array. 在第二个while循环中,在增加basket_count多维数组时遇到问题。 If you were to go through the code and write down the numbers, you'll see that you save values to [1][1], [2][2], etc. If you just want to write the five variables that correspond to the player it matches up with, then you should be using basket_count[i][k] or basket_count[i][l] since k and l are the same. 如果要遍历代码并记下数字,则会看到将值保存到[1] [1],[2] [2]等。如果只想编写对应的五个变量匹配的玩家,那么您应该使用basket_count [i] [k]或basket_count [i] [l],因为k和l相同。 You could also use a for loop instead to make it easier to limit the bounds of the loop. 您也可以使用for循环来简化限制循环的范围。 It should look more like your nested for loop at the end. 它看起来应该更像是最后的嵌套循环。

  3. Your main function should have a type of int so it returns the 0 properly. 您的main函数应具有int类型,因此它会正确返回0。 Probably not a big deal right now, but it can be much later. 现在可能不是什么大问题,但可能要晚得多。

  4. Just a few tips. 只是一些技巧。 Learn how to indent a bit better; 了解如何更好地缩进; it makes reading your code MUCH easier and helps it stay organized. 它使阅读代码变得更加容易,并有助于保持井井有条。 A small thing I noticed is that in your nested for loop, you only gave curly braces to the outside for loop. 我注意到的一件事是,在嵌套的for循环中,您仅在花括号外面加上了for循环。 Your code should work fine, but since you are a beginner, it would be best for you to put curly braces on all loops and if statements, just to be safe. 您的代码应该可以正常工作,但是由于您是初学者,因此为了安全起见,最好在所有循环和if语句上使用花括号。

Here's a way to create, populate, print and delete a simple 2D array... 这是创建,填充,打印和删除简单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;
}

I simply change your code. 我只是更改您的代码。 Your code is ok now. 您的代码现在可以了。 Lets try 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()
{
    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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM