将输入存储到数组 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19420092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:48:13  来源:igfitidea点击:

storing input into Arrays C++

c++arrays

提问by user2211678

I am learning about arrays , what I wanted to try is first let the user enter x,y values 4 times e.g

我正在学习数组,我想尝试的是首先让用户输入 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.

我知道它可以用向量来完成,但现在我正在尝试使用数组,因为我在使用数组方面很弱。

回答by Bj?rn Pollex

You are confusing a lot of things here.

你在这里混淆了很多东西。

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

回答by Mohsen Safari

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

回答by legends2k

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 xand yare 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;
    }
}

回答by Atle

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! I haven't run the code myself, let me know if it doesn't work.

附注!我自己没有运行代码,如果它不起作用,请告诉我。