简体   繁体   English

将 10 个整数存储到数组 C++ 中的问题

[英]Problem with storing 10 integers into an array C++

Ok, I'm very confused as to why this happens.好吧,我很困惑为什么会发生这种情况。 All I'm trying to do is put 10 integers from input into an array.我要做的就是将输入中的 10 个整数放入一个数组中。 Why is this happening.为什么会发生这种情况。

#include <iostream>
using namespace std;

int getData(float intArray[10]);

void printData(float intArray[10]);



int main() {
float myArray[10];
getData(myArray);
printData(myArray);

cin.get();
cin.ignore();


}

int getData(float intArray[]) {
    for (int i = 0; i < 10; i++)                                  
    {                                                        
        std::cout << "Enter a number:";
        std::cin >> intArray[10];
    }
    return 1;
}

void printData(float intArray[10]){

    cout << intArray;

    }

If you could please tell me where I'm going wrong, that would be very much appreciated.如果您能告诉我我哪里出错了,我将不胜感激。 Thank you!谢谢!

From how your code is written, you're only adding the user's input to the [10] element of intArray[] within that for loop you created.从代码的编写方式来看,您只需将用户的输入添加到您创建的for循环中intArray[][10]元素中。 Additionally, any information added to the array at intArray[10] or beyond is placed out of bounds.此外,在intArray[10]或之后添加到数组的任何信息都被放置在边界之外。

The only way I can really demonstrate what I mean is...我能真正证明我的意思的唯一方法是......

for (int i = 0; i < 10; i++)
{
    std::cout<<"Enter a number:";
    std::cin >> intArray[i];
}

Another thing I noticed is you're creating another array with the same name in your printData method.我注意到的另一件事是您在printData方法中创建了另一个具有相同名称的数组。 You should instead pass the intArray you're filling up with information to this method and use it to display your information.相反,您应该将填充信息的intArray传递给此方法,并使用它来显示您的信息。

The problem lies in this block of code-问题出在这段代码中——

for (int i = 0; i < 10; i++)                                  
    {                                                        
        std::cout << "Enter a number:";
        std::cin >> intArray[10];
    }

As mentioned in other answers and comments you are storing all the values in the 10th memory slot of the array.正如其他答案和评论中所述,您将所有值存储在数组的第 10 个内存插槽中。

As per your comment根据你的评论

I forgot to mention, the output is just random integers and characters.我忘了提,输出只是随机整数和字符。 EX: 00B3F724例如:00B3F724

00B3F724 => These are the memory address allocated to the array and which will hold the elements which will be inserted. 00B3F724 => 这些是分配给数组的内存地址,将保存将要插入的元素。

How array actually works-数组实际上是如何工作的 -

float myArray[10];

The above snip creates 10 units of memory space.上面的片段创建了 10 个单位的内存空间。 The units differ on the type which the array will hold.单位因数组将保存的类型而异。 In this case it is holding float values, so each memory space will be of 4 bytes.在这种情况下,它保存浮点值,因此每个内存空间将为 4 个字节。 All of these spaces have an address for lookup and other operations.所有这些空间都有一个用于查找和其他操作的地址。 All these spaces are expecting a float value to be inserted.所有这些空格都期望插入一个浮点值。

As you are using the loop you have to loop through the array(all the memory slots allocated to the array) and allocate a float element to each one of them and not only the last element(10th).当您使用循环时,您必须遍历数组(分配给数组的所有内存插槽)并为每个元素分配一个浮点元素,而不仅仅是最后一个元素(第 10 个)。 Effectively your for loop will become实际上,您的 for 循环将变成

 for (int i = 0; i < 10; i++)                                  
        {                                                        
            std::cout << "Enter a number:";
            std::cin >> intArray[i];
        }

Instead of intArray[10] insert values like this intArray[i] .而不是intArray[10]插入这样的值intArray[i] As i will traverse through all the slots on every iteration of the loop insert a values to a slot.由于我将遍历循环的每次迭代中的所有插槽,因此将值插入到插槽中。

Your code will look like你的代码看起来像

#include <iostream>
using namespace std;

int getData(float intArray[10]);
void printData(float intArray[10]);
int main() {
float myArray[10];
getData(myArray);
printData(myArray);
cin.get();
cin.ignore();


}

int getData(float intArray[]) {
    for (int i = 0; i < 10; i++)                                  
    {                                                        
        std::cout << "Enter a number:";
        std::cin >> intArray[i];
    }
    return 1;
}

void printData(float intArray[10]){

    cout << intArray;
    }

As you know if an array is declared as myArray[10], its index ranges from 0-9.如您所知,如果数组声明为 myArray[10],则其索引范围为 0-9。 Putting a value in myArray[10] will go out of bound and will produce garbage value.将值放入 myArray[10] 将越界并产生垃圾值。

In getData(float intArray[]) you are always overwriting the content of intArray[10] while it is out of bound so it is not being stored in an actual array.在 getData(float intArray[]) 中,当 intArray[10] 越界时,您总是会覆盖它的内容,因此它不会存储在实际数组中。 You should write your getData(float intArray[]) as following :你应该写你的 getData(float intArray[]) 如下:

int getData(float intArray[]) {
for (int i = 0; i < 10; i++)                                  
{                                                        
    std::cin >> intArray[i];
}
return 1;
}

Also in printData(float intArray[10]) you are only printing the base address of the array (ie the name of the array gives the address of the 0th index).So the correct code would be:同样在 printData(float intArray[10]) 中,您只打印数组的基地址(即数组的名称给出第 0 个索引的地址)。所以正确的代码是:

 void printData(float intArray[])
{
  for(int i=0;i<10;i++)
 {
  cout << intArray[i]<<" ";
 }
}

Simply change,简单地改变,

std::cin >> intArray[10];

to,到,

std::cin >> intArray[i];

What you are doing wrong:你做错了什么:

you are storing the value at the 10th position (actually it is 11th position) again and again and the value at 10th position replaces with the new value again and also the 10th position doesn't exist in the array because the index of the array starts from 0, so your array has the index values from 0 to 9.您一次又一次地将值存储在第 10 个位置(实际上它是第 11 个位置),第 10 个位置的值再次替换为新值,并且第 10 个位置不存在于数组中,因为数组的索引开始从 0,所以你的数组的索引值从 0 到 9。

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

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