简体   繁体   中英

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

#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. Also the n doesn't change while you fill the row.

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. 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. 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.

  2. In your second while loop, you have a problem incrementing your basket_count multidimensional array. 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. You could also use a for loop instead to make it easier to limit the bounds of the loop. 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. 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. 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.

Here's a way to create, populate, print and delete a simple 2D array...

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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