【问题标题】:Problems with putting the output in the main function in c++在 C++ 中将输出放在主函数中的问题
【发布时间】:2020-12-10 04:17:31
【问题描述】:

我想将 n / anzahl 的输入和最低和最高温度的输出放在主函数中,同时在 void 函数中进行计算。我认为我的错误是调用了错误的参考,但我看不到它。有人可以帮我看看我的错误吗?

到目前为止,我得到了这个。这段代码是主函数中的所有内容,它运行良好,但我在实现将输出放入主函数时遇到了问题

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{


    int n,i;
    int groesst, kleinst;
    int rand(void);
    cout << "Geben Sie Anzahl der Temperaturwerte an: "; //put in the amount of temperatures
    cin >> n;
    n=n+1; //so non programmers arent confused


    vector<int> temp(n);
    cout << "31 zufaellige Temperaturen:\n" << endl;
    groesst = temp[0];
    kleinst = temp[0];
     for (i=1;i<n;i++)
    {
        temp[i]=rand()%20-4;//random temperature between -4 and 15
        cout << temp[i] << " Grad Celsius"<< endl;

        if (temp[i]>groesst) //
        {
            groesst = temp[i]; //
        }

        if (temp[i]<kleinst)
        {
            kleinst = temp [i];
        }
    }


    cout << kleinst; //minimum temperature
    cout << "\n";
    cout << groesst; //maximum temperature

 return 0;
}

这是我的尝试:

#include <iostream>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iomanip>
using namespace std;

void minmaxim(vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    int i;
    int rand(void);

    temp[n];
    groesst = temp[0];
    kleinst = temp[0];
     for (i=1;i<n;i++)
    {
        temp[i]=rand()%20-4;


        if (temp[i]>groesst)
            groesst = temp[i];
        }

        if (temp[i]<kleinst)
        {
            kleinst = temp [i];
        }
        return;
    }


int main ()
{
    vector<int> temps;
    int anzahl, minimum,maximum;
    cout << "Geben Sie die Anzahl der Temperaturwerte ein: "; //type in the amounts of temperatures
    cin >> anzahl;

   minmaxim(temps, anzahl, minimum, maximum); //calling the function
   cout << " " << anzahl;

    cout << " " << minimum <<endl;
    cout << " " << maximum <<endl;

    return 0;
}

【问题讨论】:

  • 您能解释一下您遇到的问题吗?它没有编译,是打印错误的值,还是?
  • 哦 - 首先你要调整矢量的大小,例如temps.resize(n);。实际上,我认为您只是在使用未定义的内存。
  • 除了我应该输入的内容外,它没有打印任何内容,所以`“Geben Sie die Anzahl der Temperaturwerte ein:” `

标签: c++ function input output pass-by-reference


【解决方案1】:

最大的问题是minimummaximum 在调用minmaxim() 调用未定义的行为时都未初始化。在比较您的值之前,您必须将 maximumminimum 初始化为低于和高于可能温度范围的数字,例如

    int anzahl, 
        minimum =  200,     /* initialize min above and max below possible range */
        maximum = -200;

或者,正确覆盖int的整个范围,例如

#define NMAX std::numeric_limits<int>::max()
#define NMIN std::numeric_limits<int>::min()
...
    int anzahl,
        minimum = NMAX,     /* initialize min above and max below possible range */
        maximum = NMIN;

您在 C 语言中使用的随机值 rand() 已在 C++ 中替换为 Pseudo-random number generation。您将按如下方式创建和使用随机设备:

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */

然后调用

int value = dist(rd);

检索范围内的随机值。您可以设置范围的最大值或继续使用整个范围的值和模,或多或少是您的选择。

您对std::vector 的使用不太正确。 std::vector 提供 .push_back() 成员函数以添加到向量中。你的minmaxim () 函数可以写成:

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */
    
    for (int i = 0; i < n; i++) {
        int randval = dist(rd) % 20 - 4;
        temp.push_back(randval);
        
        if (randval < kleinst)
            kleinst = randval;
        if (randval > groesst)
            groesst = randval;
    }
}

Why is “using namespace std;” considered bad practice?。进行这些更改后,您可以将整个源代码编写为:

#include <iostream>
#include <vector>
#include <random>

#define NMAX std::numeric_limits<int>::max()
#define NMIN std::numeric_limits<int>::min()

void minmaxim (std::vector<int>& temp, int& n, int& kleinst, int& groesst)
{
    std::random_device rd;    /* delcare the randon number generator device */
    std::mt19937 gen(rd());   /* standard mrsene_twister engine seeded w/rd */
    std::uniform_int_distribution<int> dist(0, NMAX); /* create disribution */
    
    for (int i = 0; i < n; i++) {
        int randval = dist(rd) % 20 - 4;
        temp.push_back(randval);
        
        if (randval < kleinst)
            kleinst = randval;
        if (randval > groesst)
            groesst = randval;
    }
}


int main (void)
{
    std::vector<int> temps{};
    int anzahl,
        minimum = NMAX,     /* initialize min above and max below possible range */
        maximum = NMIN;
    
    std::cout << "Geben Sie die Anzahl der Temperaturwerte ein: ";
    if (!(std::cin >> anzahl)) {
        std::cerr << "error: invalid integer input.\n";
        return 1;
    }

    minmaxim (temps, anzahl, minimum, maximum); 
    
    for (const auto& t : temps)
        std::cout << t << '\n';
    std::cout << "\n " << anzahl << "\n " << minimum << "\n " << maximum << '\n';

    return 0;
}

注意:避免包含未使用的标题)

使用/输出示例

$ ./bin/maxmintemps
Geben Sie die Anzahl der Temperaturwerte ein: 5
4
-4
8
3
13

 5
 -4
 13

如果您想进一步整理 main() 的输出,您可以输出 10 行的温度值,结果如下。然后,您可以合法地包含&lt;iomanip&gt; 标头以将温度值与std::setw() 对齐。您可以将当前输出循环替换为:

#include <iomanip>
...
    for (size_t i = 0; i < temps.size(); i++) {
        if (i % 10 == 0)
            std::cout.put ('\n');
        std::cout << " " << std::setw(2) << temps[i];
    }
    std::cout << "\n\n anzahl  : " << anzahl << 
                "\n minimum : " << minimum << 
                "\n maximum : " << maximum << '\n';

更新输出

$ ./bin/randtempchk
Geben Sie die Anzahl der Temperaturwerte ein: 40

  3 13  9  8 12 15  1  0  7  4
 -2 -3  8 14 -4  7 -2  7 -2  2
 10  1 14  7  6  6 13  6  6  0
  5  9  6  8 13  9 14  9 15  6

 anzahl  : 40
 minimum : -4
 maximum : 15

查看一下,如果您还有其他问题,请告诉我。

【讨论】:

  • 感谢您所做的一切!!我应该这么说,但我对编程还不是很熟悉,所以我不确定你带来的这些东西中的大部分是什么意思。我认为我们没有“分发”或“后推”或这个随机数发电机在我们的讲座呢。不使用这些有什么办法解决这个问题?
  • 咯咯笑——刚开始的时候没人知道这些东西。学习 C++,就像吃鲸鱼一样——一次只吃一个字节:)。有关随机生成,请参阅UniformRandomBitGenerator。 (也只需为cppreferece.com 网站添加书签)。这是最好的参考。即使是现在,当我需要处理一个元组时,我也会去std::tuple 并刷新语法以及如何将内容合二为一,然后再退出。所以一次一个字节:)
  • 在 cppreference 站点,您可以使用搜索框找到您需要的大部分内容。要从元组页面到向量页面,只需在搜索框中输入std::vector。 (如果你不知道你要找的确切的东西——那么搜索很糟糕,所以只需从该网站主页上的链接开始,你通常可以找到你需要的东西。在堆栈溢出时搜索这里,只需使用 "[c++] your search terms" 将搜索范围限制为标记为 [c++] 的问题。此处和 cppreference 提供了很好的信息 - 但对您在其他地方找到的内容持怀疑态度。祝您好运。
猜你喜欢
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 2021-04-01
  • 2021-05-15
  • 2021-09-25
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多