简体   繁体   English

调用void函数时cout没有输出

[英]No output from cout when calling void function

I'm new to C++ so bear with me. 我是C ++的新手,请多多包涵。

I am trying to create a histogram from certain parameters (interval size, length of array containing quantities of numbers, highest number yadayada). 我正在尝试根据某些参数(间隔大小,包含数字数量的数组的长度,最高数量的yadayada)创建直方图。

The details are irrelevant and a problem for myself to fiddle with, although I think I got the correct formula in my function. 尽管我认为我的函数中使用了正确的公式,但这些细节无关紧要,也是我自己不易解决的问题。

When I assign variables from the C++ IO "cin" I can output those with the "cout" call, however, when I call my histogram function, also containing "cout" instructions, nothing gets printed. 当我从C ++ IO的“ cin”分配变量时,可以通过“ cout”调用输出变量,但是,当我调用还包含“ cout”指令的直方图函数时,则不会打印任何内容。

My code: 我的代码:

#include <iostream>
#include <cmath>

using namespace std;

void histogram(int l, int n, int k, int *a)
{
        int quantity = 0;
        for (int i = 1; i <= l; i++)
        {
                for (int j = 0; i < n; j++)
                {
                        if (a[j] >= (i-1) * k || a[j] <= i * k)
                        {
                                quantity++;
                        }
                }

                cout << (i-1) * k + ": " + quantity << endl;
                quantity = 0;
        }
}


int main()
{
        int l,n,k;
        int *a;

        a = new int[n];

        cin >> l >> n;

        for (int i = 0; i < n; i++)
        {
                cin >> a[i];
        }

        k = ceil((double)a[0]/l);

//      cout << k;

        histogram(l,n,k,a);

        return 0;
}

There might be something funky going on with this line and string concatenation: cout << (i-1) * k + ": " + quantity << endl; 这行和字符串的连接可能有些时髦: cout << (i-1) * k + ": " + quantity << endl; You might try rewriting as cout << ((i-1) * k) << ": " << quantity << endl; 您可以尝试将其重写为cout << ((i-1) * k) << ": " << quantity << endl; just to ensure that things are adding and concatenating correctly. 只是为了确保事物正确地添加和连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM