C++ 函数c ++中的用户输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39908477/
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-28 14:52:00  来源:igfitidea点击:

User input array in Function c++

c++arraysfunctionparameters

提问by Jeroen

I am trying to figure out how I can put this code in a function, so I can call it back in my main, but nothing is working. I am quite new to programming, thanks in advance! (So the goal is to let the user input numbers and the highest one gets picked out).

我想弄清楚如何将这段代码放在一个函数中,这样我就可以在主函数中调用它,但没有任何效果。我对编程很陌生,提前致谢!(所以目标是让用户输入数字并选出最高的数字)。

for (z = 0; z < 10; z++) {
    cin >> array[z];


    for (int i = 0; i < 10; i++)
    {
        if (array[i] > temp)
            temp = array[i];
    }

}

回答by 4rwsAwper

it looks like this should be two different "for" loops

看起来这应该是两个不同的“for”循环

you forgot to declare z, temp and array[]

你忘了声明 z、temp 和 array[]

EDIT: don't forget if you initialize array[] within the function you need to include the aggregate

编辑:不要忘记,如果您在需要包含聚合的函数中初始化 array[]

try this:

尝试这个:

int z, i;
int temp = 0;
int array[10] = {0,0,0,0,0,0,0,0,0,0};

for (z = 0; z < 10; z++) {
    cin >> array[z];
}


for (i = 0; i < 10; i++)
{
    if (array[i] > temp)
        temp = array[i];
}

this function would take no arguments, so you would declare it and call it as "function()"

此函数不带任何参数,因此您可以声明它并将其称为“function()”

SECOND EDIT: i was actually able to get a function like this to work, and it would look like this:

第二次编辑:我实际上能够让这样的函数工作,它看起来像这样:

#include <iostream>

using namespace std;

void function();

int main() {
    cout << "enter 10 numbers: " << endl;

    function();

    return 0;
}

void function () {
    int z, i;
    int temp = 0;
    int array[10] = {0,0,0,0,0,0,0,0,0,0};

    for (z = 0; z < 10; z++) {
    cin >> array[z];
    }


    for (i = 0; i < 10; i++) {
        if (array[i] > temp)
            temp = array[i];
    }

    cout << "your largest number is: " << temp;
}

good luck out there man

祝你好运,伙计