简体   繁体   English

C ++ Visual Studio无法完全运行程序

[英]C++ Visual Studio Won't fully run program

I have this program, and I felt like I had finally made my code correct, but my output would never print my asterisks. 我有这个程序,感觉好像我终于使我的代码正确了,但是我的输出永远不会打印出星号。 A buddy of mine told me to run it through another compiler. 我的一个伙伴告诉我要通过另一个编译器运行它。 I used JDoodle and my code worked perfectly. 我使用了JDoodle,我的代码运行良好。 No matter what, I can't get it to run properly in Visual Studio. 无论如何,我无法使其在Visual Studio中正常运行。 I updated it and everything. 我更新了所有内容。 Anyone have any advice at all? 有人有什么建议吗? I'll leave my code here, and would love to know if it has issues in someone elses Visual Studio, or if I am having issues with my compiler. 我将代码留在这里,很想知道它在其他Visual Studio中是否存在问题,或者我的编译器是否存在问题。 Thanks in advance. 提前致谢。

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

int* histo(int* scores, int count);
double dev(int count, int* scores);
double mean(int count, int* scores);

int main()
{
    int scores[101];
    int count = 0;

    cout << "Enter a score (-1 to stop): ";

    do
    {
        cin >> scores[count++];
    } while (scores[count - 1] != -1);
    count--;

    int* bins = histo(scores, count);

    for (int i = 9; i >= 0; i--)
    {
        cout << i << "| ";

        for (int j = 0; j < bins[i]; j++)
        {
            cout << "*";
        }
        cout << endl;
    }

    delete[] bins;

    cout << "mean: " << mean(count, scores) << endl;
    cout << "dev: " << dev(count, scores) << endl;  
    system("pause");
    return 0;
}

int* histo(int* scores, int count)
{
    int* bins = new int[10];

    for (int i = 0; i < count; i++)
    {
        if (scores[i] < 10)
        {
            bins[0]++;
        }
        else if (scores[i] >= 10 && scores[i] < 20)
        {
            bins[1]++;
        }
        else if (scores[i] >= 20 && scores[i] < 30)
        {
            bins[2]++;
        }
        else if (scores[i] >= 30 && scores[i] < 40)
        {
            bins[3]++;
        }
        else if (scores[i] >= 40 && scores[i] < 50)
        {
            bins[4]++;
        }
        else if (scores[i] >= 50 && scores[i] < 60)
        {
            bins[5]++;
        }
        else if (scores[i] >= 60 && scores[i] < 70)
        {
            bins[6]++;
        }
        else if (scores[i] >= 70 && scores[i] < 80)
        {
            bins[7]++;
        }
        else if (scores[i] >= 80 && scores[i] < 90)
        {
            bins[8]++;
        }
        else if (scores[i] >= 90)
        {
            bins[9]++;
        }
    }
    return bins;
}

double dev(int count, int* scores)
{
    double x = 0;
    double y = 0;
    double std_dev = 0;
    double mean_t;

    mean_t = mean(count, scores);

    for (int i = 0; i < count; i++)
    {
        x = pow(scores[i] - mean_t, 2);
        y += x;
    }
    std_dev = sqrt(y / count);
    return std_dev;
}
double mean(int count, int* scores)
{
    double total = 0;

    for (int i = 0; i < count; i++)
    {
        total += scores[i];
    }

    return total / count;
}

You seem to assume that automatic variables on stack/heap are initialized to 0, they are not. 您似乎假设堆栈/堆上的自动变量已初始化为0,但不是。 Eg int* bins = new int[10]; 例如int* bins = new int[10]; gives you an array of 10 uninitialized integers that could have any value, which could explain why it works with one compiler and the other not. 为您提供10个未初始化整数的数组,这些数组可以具有任何值,这可以解释为什么它与一个编译器一起工作而对另一个编译器不工作。

So potentially your histo function will return an array with garbage. 因此,您的histo函数可能会返回带有垃圾的数组。 eg -1, -1337, ... 例如-1,-1337,...

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

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