首页 > 解决方案 > 将输出放在 c++ 中的 main 函数中的问题

问题描述

我想将 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;
}

标签: c++functioninputoutputpass-by-reference

解决方案


最大的问题是调用未定义的行为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;
    }
}

请参阅为什么是“使用命名空间标准;” 被认为是不好的做法?. 进行这些更改后,您可以将整个源代码编写为:

#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 行输出温度值,结果如下。然后,您可以合法地包含<iomanip>标题以将温度值与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

如果您还有其他问题,请仔细查看并告诉我。


推荐阅读