简体   繁体   English

将输入存储到Arrays C ++中

[英]storing input into Arrays C++

I am learning about arrays , what I wanted to try is first let the user enter x,y values 4 times eg 我正在学习数组,我想尝试的是首先让用户输入x,y值4次,例如

first time 第一次

x = 1
y = 3

second time 第二次

 x = 2
 y = 3

third time 第三次

 x = 3
 y = 1

fourth time 第四次

 x = 1
 y = 3

. and then store the value that the user had key in 4 times inside a array and print them out but I getting some weird outputs. 然后将用户键入的值存储在数组中4次并打印出来但我得到了一些奇怪的输出。

my output 我的输出

10001711642800 <-- some weird output

expected output 预期产出

1,3
2,3
3,1
1,3

code(not working) 代码(不工作)

      int x;
      int y;

     //request the user to enter x and y value 4 times.
     for (int i=1; i<5; i++) {
        cout << i << "Please enter x-cord." << endl;
        cin >> x;

        cout <<i << "Please enter y-cord." << endl;
        cin >> y;
    }
     //intitalize the array size and store the x,y values   
    int numbers[4][4] = { 
        x, y
    };
    //loop through 4 times to print the values.
    for (int i = 0; i<5; i++) {
        cout << numbers[i][i];
    }

I know it can be done with vectors but now I am trying with arrays because I am weak in using arrays. 我知道它可以用向量完成,但现在我正在尝试使用数组,因为我在使用数组方面很弱。

You are confusing a lot of things here. 你在这里混淆了很多东西。

  1. In your for -loop you are overwriting the value stored in x and y at each iteration of the loop. for -loop中,您将在循环的每次迭代中覆盖存储在xy中的值。
  2. int numbers[4][4] creates a two-dimensional array containing a total of 16 elements. int numbers[4][4]创建一个包含总共16个元素的二维数组。 What you want is int numbers[4][2] . 你想要的是int numbers[4][2]
  3. Your array initialization is incomplete, because x and y only contain the last two values the user has entered, not all 8. 您的数组初始化不完整,因为xy仅包含用户输入的最后两个值,而不是全部8。

To fix this, you should create the array before the for -loop and then store the values the user enters directly into the array. 要解决此问题,您应该在for -loop之前创建数组,然后将用户直接输入的值存储到数组中。

use this code: 使用此代码:

int numbers[4][2];
for(int i=0;i<4;i++)
{
   cout<<i<<": Please enter x-cord."<<endl;
   cin>>numbers[i][0];
   cout<<i<<": Please enter y-cord."<<endl;
   cin>>numbers[i][1];
}

for (int i = 0; i<4; i++) 
{
        cout << numbers[i][0]<<"   "<<numbers[i][1];
}

You're not storing the input received from the user in any array. 您不是将从用户收到的输入存储在任何阵列中。 They're overwritten again and again in the loop. 它们在循环中一次又一次地被覆盖。 Store them in an array and then display it. 将它们存储在一个数组中然后显示它。

 //intitalize the array size and store the x,y values   
int numbers[4][4] = { 
    x, y
};

This is not required. 这不是必需的。 Since you're going to overwrite the contents of the array you needn't initialize them with some random variables. 由于您要覆盖数组的内容,因此无需使用一些随机变量对其进行初始化。 In fact the variables x and y are not at all required. 事实上,变量xy根本不是必需的。

#include <iostream>

int main(int argc, char *argv[])
{
    // you need 4 arrays of 2 numbers, not 4x4 but 4x2
    int numbers[4][2] = { { } };        // initialize all of them to 0
    for (size_t i = 0; i < 4; ++i)
    {
        std::cout << "x = ";
        std::cin >> numbers[i][0];      // store the input directly in the array
        std::cout << "y = ";            // instead of using a dummy
        std::cin >> numbers[i][1];
    }

    // display array contents
    for (size_t i = 0; i < 4; ++i)
    {
        std::cout << numbers[i][0] << ", " << numbers[i][1] << std::endl;
    }
}

First, you have to declare the variable you need to fill; 首先,您必须声明需要填充的变量;

 // A new data type which holds two integers, x and y
 struct xy_pair {
     int x;
     int y;
 };

 // A new array of four xy_pair structs
 struct xy_pair numbers[4];

Then, you can begin filling it. 然后,您可以开始填写它。

 //request the user to enter x and y value 4 times.
 for (int i=1; i<5; i++) {
    cout << i << "Please enter x-cord." << endl;
    cin >> numbers[i].x;

    cout <<i << "Please enter y-cord." << endl;
    cin >> numbers[i].y;
}

Then, you can print it! 然后,你可以打印它!

//loop through 4 times to print the values.
for (int i = 0; i<5; i++) {
    cout << "X is " << numbers[i].x << " and Y is " << numbers[i].y << endl;
}

PS! PS! I haven't run the code myself, let me know if it doesn't work. 我自己没有运行代码,如果它不起作用,请告诉我。

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

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