【问题标题】:How can I input data to array and print them through function?如何将数据输入数组并通过函数打印它们?
【发布时间】:2019-08-22 19:04:05
【问题描述】:

我正在尝试将数据输入到数组中,然后使用函数打印它们,但我不确定如何组织代码。

#include <iostream>
using namespace std;

void showGrade(double grade[], int size);

int main()
{
    double grade[];
    int size;

    for (int i = 0; i < size; i++) 
    {
        cout << "Please enter the number of grade" << endl;
        cin >> size;
    }

    showGrade(grade, size);
    return 0;
}

void showGrade(double grade[], int size)    //How many grade we have
{
    for (int counter = 0; counter < size; counter++) 
    {
        cout << "Please enter your grades: " << endl;
        cin >> grade[counter];
        cout << "Here are your grades: " << endl;
    }
}

我希望看到我输入了多少grades,然后显示出来。

2019 年 8 月 28 日更新

我想出了如何在 main 函数中成功地做到这一点。但我真正想要的是把它们放在一个单独的函数中。我的新代码在函数调用中有错误,即类型名称是不允许的,需要一个')'。如何让它发挥作用?

#include <iostream>
using namespace std;

void showGrades(double ar[], int size);

int main()
{
    double ar[20];
    int size;

    showGrades(double ar[size], int size);

    system("pause");
    return 0;
}

void showGrades(double ar[], int size) {
    cout << "Please enter the number of grade "; // array size
    cin >> size;
    cout << "Please enter your grades " << endl;
    for (int i = 0; i < size; i++) {
        cin >> ar[i];
    }

    cout << "The grades you entered are: " << endl;

    for (int i = 0; i < size; i++) {
        cout << ar[i] << endl;
    }
}

【问题讨论】:

  • double grade[]; std::vector。
  • int size; for (int i = 0; i &lt; size; i++) { - 这永远行不通。 size 未初始化。
  • using namespace std; - 这通常是个坏主意。
  • 不相关:我不希望名为 showGrade 的函数从成绩中获取输入。我希望它只显示成绩。应该创建一个InputGrades 函数来处理输入任务。一般来说,一个函数应该只做一件事(即使那一件事是聚合一堆函数的结果,每个函数都做一件事)并根据它所做的一件事命名。这使您可以构建非常简单且易于测试的函数,作为更复杂程序的构建块,随时测试所有内容。
  • 哦,我以为我已经初始化了大小。那么我应该在哪里做呢?在主函数中?

标签: c++ arrays algorithm function user-input


【解决方案1】:
  1. 首先需要使用标准容器 std::vector 而不是double grade[];,因为你想要一个可变长度 根据用户输入的数组。

  2. 其次,您在

    中使用了未初始化的size变量
    for (int i = 0; i < size; i++) 
    

    因此它将使用垃圾值初始化。你不需要for-loop

一个好的开始应该是:

#include <iostream>
#include <vector>    // std::vector
void showGrade(std::vector<double>& grade)
//            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -> pass the vector by ref, as the grades should be inseted to the it
{
    // the logic
}

int main()
{
    int size;
    std::cout << "Please enter the number of grade" << endl;
    std::cin >> size;

    std::vector<double> grade;
    grade.reserve(size); // reserve memory for unwanted re-allocations

    showGrade(grade);
    return 0;
}

在阅读了有关std::vector 的更多信息后,我将其留给您完成。

另外,不要用using namespace std; 练习。阅读更多: Why is "using namespace std;" considered bad practice?

【讨论】:

  • 感谢您的建议,我将阅读更多如何使用矢量,因为我不关心它。
  • @MinhNguyen 欢迎您:对以下内容进行简短阅读可能会很有趣:How to add an element in Vector using vector::push_backHow to print out the contents of a vector?
  • 我想出了如何在主函数中成功地做到这一点。但我真正想要的是把它们放在一个单独的函数中。我的新代码在函数调用中有错误,即类型名称是不允许的,需要一个')'。如何让它发挥作用?
  • 我在原代码下面更新了。只需向下滚动一点。
  • 没关系,我通过在函数原型和函数声明中添加&找到了方法。谢谢。
【解决方案2】:

这不是有效的 C++ 代码:

double grade[];

你可以使用std::vector:

std::vector<double> grade;

要在向量中插入等级,您可以使用grade.push_back(someGrade);

【讨论】:

  • 不确定这是否能回答问题。应该改为评论。
  • 谢谢,我还不知道如何使用矢量,但我会阅读更多相关信息。
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多