I';我在C+方面有困难+;获取用户在函数中的输入、添加到数组并打印该数组 我正在学习C++,我想让用户输入一个函数中的4个数字,然后简单地打印数组。 int getFourNums(); int main(int argc, char** argv){ int getNums; getNums = getFourNums(); cout << "The array is: " getNums << endl; } int getFourNums(){ int i; int myArray[4]; cout << "Enter 4 nums: "; for(i = 0; i < 4; i++){ cin >> myArray[i]; } return myArray[i]; intgetfournums(); int main(int argc,字符**argv){ int getNums; getNums=getFourNums(); cout

I';我在C+方面有困难+;获取用户在函数中的输入、添加到数组并打印该数组 我正在学习C++,我想让用户输入一个函数中的4个数字,然后简单地打印数组。 int getFourNums(); int main(int argc, char** argv){ int getNums; getNums = getFourNums(); cout << "The array is: " getNums << endl; } int getFourNums(){ int i; int myArray[4]; cout << "Enter 4 nums: "; for(i = 0; i < 4; i++){ cin >> myArray[i]; } return myArray[i]; intgetfournums(); int main(int argc,字符**argv){ int getNums; getNums=getFourNums(); cout,c++,arrays,function,user-input,C++,Arrays,Function,User Input,代码中的一个问题是 for(i = 0; i < 4; i++){ cin >> myArray[i]; } 代码中的一个问题是 for(i = 0; i < 4; i++){ cin >> myArray[i]; } 您的基本问题是intgetfournums()只能返回一个整数,不能返回它们的数组。下一个问题是函数由于历史原因无法返回原始数组。您可以选择返回包含数组的std::array、结构,通过引用将数组传递到函数中,或返回std:

代码中的一个问题是

for(i = 0; i < 4; i++){
    cin >> myArray[i];
}

代码中的一个问题是

for(i = 0; i < 4; i++){
    cin >> myArray[i];
}

您的基本问题是
intgetfournums()
只能返回一个整数,不能返回它们的数组。下一个问题是函数由于历史原因无法返回原始数组。您可以选择返回包含数组的
std::array
、结构,通过引用将数组传递到函数中,或返回
std::vector
或者此应用程序是一个
std::vector
-它很灵活,虽然不如
std::array
那么高效,但除非您有充分的理由,否则您可能应该默认使用
std::vector
。您的
getNums
代码如下所示:

std::vector<int> getFourNums() {
    std::vector<int> result;
    cout << "Enter 4 nums: ";
    for(int i = 0; i < 4; i++){
        int v;
        cin >> v;
        result.push_back(v);
    }
    return result;
}
std::vector getFourNums(){
std::向量结果;
cout>v;
结果:推回(v);
}
返回结果;
}

要打印矢量,请参见。我个人的偏好是基于范围的for循环,而不是向量;您的喜好可能会有所不同。

您的根本问题是
int getFourNums()
只能返回一个整数,而不能返回它们的数组。下一个问题是,由于历史原因,函数无法返回原始数组。您可以选择返回一个
std::array
,一个包含数组的
struct
,通过引用将数组传递到函数中,或者返回一个
std::vector
。我对这个应用程序的偏好是一个
std::vector
——它很灵活,虽然不如
std::array
那么有效,但除非您有充分的理由,否则您可能应该默认使用
std::vector
。您的
getNums
代码将如下所示:

std::vector<int> getFourNums() {
    std::vector<int> result;
    cout << "Enter 4 nums: ";
    for(int i = 0; i < 4; i++){
        int v;
        cin >> v;
        result.push_back(v);
    }
    return result;
}
std::vector getFourNums(){
std::向量结果;
cout>v;
结果:推回(v);
}
返回结果;
}
要打印矢量,请参见。我个人的偏好是基于范围的for循环,而不是向量;您的喜好可能会有所不同。

int-getFourNums()
只允许您返回一个
int
,而不是整个数组,
返回myArray[i]i==4
,代码>超出范围。只能将范围[0,3]用作数组的索引。这是一个代码中带有注释的修改版本

#include <iostream>
#include <vector>

// don't do "using namespace std;" since it includes
// a lot of stuff you don't need.

// Here's a function that will return a vector of int's
// It'll behave much like a C style array
// but can have variable length and you can use
// a lot of standard functions on it.

std::vector<int> getNums(size_t count) {

    // The "array" we'll return with "count" number of
    // default constructed int:s (they will all be 0):

    std::vector<int> myArray(count);

    std::cout << "Enter " << count << " nums: ";

    // A range based for loop that will go through
    // all int:s in "myArray". "num" will be
    // a reference to each int in the vector which
    // means that if you change the value of "num",
    // you'll actually change the value in the vector.

    for(int& num : myArray) {
        // read values into the int currently
        // referenced by num

        std::cin >> num;
    }

    // return the vector by value
    return myArray;
}

// Put main() last so you don't have to forward declare the functions
// it uses

int main() {

    // call getNums with the value 4 to read 4 int:s
    std::vector<int> Nums = getNums(4);

    std::cout << "The array is:";

    // print each int in the vector. There's no need to use
    // a reference to the int:s here since we won't be changing
    // the value in the vector and copying an int is cheap.

    for(int num : Nums) {
        std::cout << " " << num;
    }

    // std::endl is rarely good when you only want to output a newline.
    // It'll flush the buffer with is costly.
    // Make a habit of using "\n" in most cases.

    std::cout << "\n";
}
#包括
#包括
//不要“使用命名空间std”,因为它包括
//很多你不需要的东西。
//这是一个返回int向量的函数
//它的行为很像一个C风格的数组
//但是可以有可变长度,并且可以使用
//上面有很多标准函数。
标准::向量getNums(大小\u t计数){
//我们将返回的“数组”包含
//默认构造的int:s(它们都将为0):
std::向量myArray(计数);
std::cout
int getFourNums()
将只允许您返回一个
int
,而不是整个数组,
返回myArray[i];
是不允许的,因为
i==4
。您只能使用范围[0,3]作为数组的索引。这是一个在代码中带有注释的修改版本

#include <iostream>
#include <vector>

// don't do "using namespace std;" since it includes
// a lot of stuff you don't need.

// Here's a function that will return a vector of int's
// It'll behave much like a C style array
// but can have variable length and you can use
// a lot of standard functions on it.

std::vector<int> getNums(size_t count) {

    // The "array" we'll return with "count" number of
    // default constructed int:s (they will all be 0):

    std::vector<int> myArray(count);

    std::cout << "Enter " << count << " nums: ";

    // A range based for loop that will go through
    // all int:s in "myArray". "num" will be
    // a reference to each int in the vector which
    // means that if you change the value of "num",
    // you'll actually change the value in the vector.

    for(int& num : myArray) {
        // read values into the int currently
        // referenced by num

        std::cin >> num;
    }

    // return the vector by value
    return myArray;
}

// Put main() last so you don't have to forward declare the functions
// it uses

int main() {

    // call getNums with the value 4 to read 4 int:s
    std::vector<int> Nums = getNums(4);

    std::cout << "The array is:";

    // print each int in the vector. There's no need to use
    // a reference to the int:s here since we won't be changing
    // the value in the vector and copying an int is cheap.

    for(int num : Nums) {
        std::cout << " " << num;
    }

    // std::endl is rarely good when you only want to output a newline.
    // It'll flush the buffer with is costly.
    // Make a habit of using "\n" in most cases.

    std::cout << "\n";
}
#包括
#包括
//不要“使用命名空间std”,因为它包括
//很多你不需要的东西。
//这是一个返回int向量的函数
//它的行为很像一个C风格的数组
//但是可以有可变长度,并且可以使用
//上面有很多标准函数。
标准::向量getNums(大小\u t计数){
//我们将返回的“数组”包含
//默认构造的int:s(它们都将为0):
std::向量myArray(计数);

std::cout我看到您希望返回整个数组,但只需查看您的返回类型:

int getFourNums()
您返回的是一个整数,对吗?在这种情况下,返回的整数始终是
myArray[4]
。请注意,它是一个整数值,您返回的东西实际上不属于您

那么该怎么办呢?我建议您将数组传递到如下函数:

 void getFourNums(int myArray[]){
    int i;
    cout << "Enter 4 nums: ";
    for(i = 0; i < SIZE; i++){
        cin >> myArray[i];
    }
}
#include <iostream>
using namespace std;
const int SIZE = 4;

void getFourNums(int myArray[]);
void printFourNumbers(int array[]);

int main(int argc, char** argv){
    int myArray[SIZE];
    getFourNums(myArray);
    printFourNumbers(myArray);

}

void getFourNums(int myArray[]){
    int i;
    cout << "Enter 4 nums: ";
    for(i = 0; i < SIZE; i++){
        cin >> myArray[i];
    }
}

void printFourNumbers(int array[])
{
    for(int i = 0 ; i < SIZE ; ++i)
    {
        cout << array[i] << endl;
    }
}
void getFourNums(int-myArray[]{
int i;
cout>myArray[i];
}
}
现在你填充了你的数组。那么如何打印你的数组呢?我们不能简单地给出我们的数组名称,然后告诉cout像你那样打印它(实际上你不能!)。这里没有什么神奇的。我们将一个接一个地打印你的数组元素:

void printFourNumbers(int array[])
{
    for(int i = 0 ; i < SIZE ; ++i)
    {
        cout << array[i] << endl;
    }
}
void printFourNumbers(int数组[])
{
对于(int i=0;i我看到您希望返回整个数组,但只需查看返回类型:

int getFourNums()
您返回的是一个整数,对吗?在这种情况下,返回的整数始终是
myArray[4]
。请注意,它是一个整数值,您返回的东西实际上不属于您

那么该怎么办呢?我建议您将数组传递到如下函数:

 void getFourNums(int myArray[]){
    int i;
    cout << "Enter 4 nums: ";
    for(i = 0; i < SIZE; i++){
        cin >> myArray[i];
    }
}
#include <iostream>
using namespace std;
const int SIZE = 4;

void getFourNums(int myArray[]);
void printFourNumbers(int array[]);

int main(int argc, char** argv){
    int myArray[SIZE];
    getFourNums(myArray);
    printFourNumbers(myArray);

}

void getFourNums(int myArray[]){
    int i;
    cout << "Enter 4 nums: ";
    for(i = 0; i < SIZE; i++){
        cin >> myArray[i];
    }
}

void printFourNumbers(int array[])
{
    for(int i = 0 ; i < SIZE ; ++i)
    {
        cout << array[i] << endl;
    }
}
void getFourNums(int-myArray[]{
int i;
cout>myArray[i];
}
}
现在你填充了你的数组。那么如何打印你的数组呢?我们不能简单地给出我们的数组名称,然后告诉cout像你那样打印它(实际上你不能!)。这里没有什么神奇的。我们将一个接一个地打印你的数组元素:

void printFourNumbers(int array[])
{
    for(int i = 0 ; i < SIZE ; ++i)
    {
        cout << array[i] << endl;
    }
}
void printFourNumbers(int数组[])
{
对于(int i=0;i您的错误是否在这里
返回myArray[i];
您的代码试图返回位于
myArray[4]的单个整数
这是超出范围的。你不打印数组,是吗?相关:如果你想返回一个数组
int getNums;
将不起作用,因为这是一个整数而不是数组。执行此操作的
c++
方法是使用
std::array
std::vector
altho