简体   繁体   English

用户输入数组程序不断返回255

[英]user input array program keeps returning 255

I might have done something wrong. 我可能做错了什么。 All I know is that the program keeps crashing when it should be getting to display the array. 我所知道的是,当程序应该显示数组时,它总是崩溃。

This is the code: 这是代码:

#include <iostream>

using namespace std;

int findSmallestRemainingElement(int input_values[], int size, int index);

void swap(int input_values[], int first_index, int second_index);

void insertionSort(int input_values[], int size);

void displayArray(int input_values[], int size);

int* userInput(int input_values[], int size);

int main()
{
    int input_values[50];
    userInput(input_values, 50);
    for (int i = 0; i < 50; i++)
    {
        cout << input_values[i];
    }
    insertionSort(input_values, 50);
    displayArray(input_values, 50);
    cout << "\n";
}

void insertionSort(int input_values[], int size)
{
    for (int i = 0; i < size; i++)
    {
        int index = findSmallestRemainingElement(input_values, size, i);
        swap(input_values, i, index);
    }
}

int findSmallestRemainingElement(int input_values[], int size, int index)
{
    int index_of_smallest_value = index;
    for (int i = index + 1; i < size; i++)
    {
        if (input_values[i] < input_values[index_of_smallest_value])
        {
            index_of_smallest_value = i;
        }
    }
    return index_of_smallest_value;
}

void swap(int input_values[], int first_index, int second_index)
{
    int temp = input_values[first_index];
    input_values[first_index] = input_values[second_index];
    input_values[second_index] = temp;
}

void displayArray(int input_values[], int size)
{
    cout << input_values[0] << endl;

    cout << input_values[24] << endl;

    cout << input_values[49] << endl;

    for (int i = 0; i < size; i++)
    {
        if (i != 0)
        {
            cout << "\n";
        }
        cout << input_values[i];
        cout << "\n";
    }
}

int* userInput(int input_values[], int size)
{
   int user_input[50];
   for (int i = 0; i <= 50; i++)
    {
       cout << "Provide input: ";
       cin >> user_input[50];
    }
    return user_input;
}

The program keeps crashing with error code 255 unless I stop it early (in which it returns some weird negative value). 除非我提早停止程序,否则该程序始终崩溃,并显示错误代码255(在该错误代码中,它返回一些奇怪的负值)。

The program is a solution for a Practice Problem in Jumping into C++, on sorting arrays (namely, it's the Insertion Sort). 该程序是对数组排序(即插入排序)中的“跳入C ++”中的一个实践问题的解决方案。

This is the problem itself: 这就是问题本身:

Write a program that takes in 50 values and prints out the highest, the lowest, the average and then all 50 input values, one per line. 编写一个程序,该程序接受50个值,并打印出最高,最低,平均值,然后打印所有50个输入值,每行一个。

I've edited this post with the latest update to the code. 我用最新的代码更新编辑了这篇文章。 I just keep getting this weird array output even after including the return statement 即使在包含return语句之后,我仍然不断得到这个奇怪的数组输出

return user_input;

So what's with this? 那么这是怎么回事? It just gives me the warning: 它只是给我警告:

warning: address of local variable 'user_input' returned [enabled by default] 警告:返回本地变量“ user_input”的地址[默认启用]

concerning the return statement. 关于退货声明。

You do this: 你做这个:

userInput(input_values[50], 50) userInput(input_values [50],50)

which should be 应该是

userInput(input_values, 50) userInput(input_values,50)

you do this multiple times. 您多次执行此操作。 Second remark: you can also take input in a loop, in the same way as you do cout in the main part of your program. 第二点:您也可以在循环中接受输入,就像在程序主要部分执行cout一样。

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

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