exe运行后停止工作

program.exe has stopped working after running it

本文关键字:停止工作 运行 exe      更新时间:2023-10-16

我不知道我有什么问题。我构建没有错误,一切都很好。老实说,我得到了这个项目。然而,我的教授不允许使用全局变量。因此,我将变量放入main函数和其他void函数中。然而,当我运行它时,它说"program.exe已停止工作"。我不知道该怎么补救。我可能在某个地方用错了指针,或者我可能计算错了标准差。我不知道。任何帮助都将是感激的。非常感谢!!这是我的输入文件"scores.txt":

46

85 100 90 85 97 92 72 88 79 86 97 89 67 96 84 96 93 77 56 77 85 100 84<<p>

>下面是我的代码:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
const int SIZE =46; // This is the maximum student's scores

// Here is my function prototyoe
void readScores   ( int array[], int *pLast);
void insertionSort( int *ptr, int SIZE);
void displayArray ( int array[], int *pLast);
double calAverage ( int array[], int *pLast);
int *calLowest    ( int array[], int *pLast);
int *calHighest   ( int array[], int *pLast);
double calStandDevi (int array[], int *pLast);
void printResult   ( int array[], int *pLast);
int main()
{
    int num_scores;
    int array[SIZE];
    int *ptr = array;      // Here is my pointer that point to the array
    int *pLast = array + SIZE-1;    // Here is my last pointer.
    // I need to initialize the average, lowest, highest scores and stand deviation so that I can call my function.
    double avg, stand_deviation;
    int *get_lowest, *get_highest;
    // to open, work and save my file. I need to initialize this.
    ifstream inputFile;
    ofstream outputFile;
    cout << "Welcome to my third program!!!" << endl;
    cout << endl;
    // Here is my eight function call.
    readScores( array, pLast);
    insertionSort(&array[0],SIZE);
    displayArray(array,pLast);
    avg = calAverage(array,pLast);
    get_lowest = calLowest(array,pLast);
    get_highest = calHighest(array,pLast);
    stand_deviation = calStandDevi(array,pLast);
    printResult( array, pLast);
    //cout << num_scores << endl; // just the test if I read the correct scores from the inputfile

    return 0;
}
// Here is my first function definition which is read scores from the input file.
void readScores ( int array[], int *pLast )
{
    int num_scores;
    ifstream inputFile;
    inputFile.open("scores.txt");   // I open the file.
    inputFile >> num_scores;        // I read the file.
    for ( int *ptr = array; ptr <= pLast; ptr++)
        {
            inputFile >> *ptr;
        }
        inputFile.close();  // I close the file.
}
// Here is my second function definition to sort the array in ascending order using Insertion Sort.
void insertionSort(int *ptr, int SIZE)
{
   for (int curr =1; curr < SIZE; curr++)
       {
           int hold = *(ptr+curr);
           int back = *(ptr-1+curr);
             while( hold < *(ptr-1+curr) && (curr > 0))
                {
                    *(ptr+curr)= *(ptr-1+curr);
                    curr--;
                }
            *(ptr+curr) = hold;
       }
}
// Here is my third function definition which  sorted array 10 numbers per line.
void displayArray ( int array[], int *pLast)
{
    int count =0;
    for (int *ptr = array; ptr <= pLast; ptr++)
    {
        count++;
        cout << *ptr << " ";
        if ( count % 10 == 0)
        {
        cout << endl;
        }
    }
    cout << endl;
}
// Here is my fourth function definition which calculate the average.
double calAverage( int array[], int *pLast)
{
    double sum1 =0;
    double avg;
    for ( int *ptr = array; ptr <= pLast; ptr++)
       {
        sum1 += *ptr;
        avg = sum1/SIZE;
       }
       return avg;
}
// Here is my fifth function definition which calculate the lowest score.
int *calLowest ( int array[], int *pLast)
{
    int *get_lowest = array;
    for ( int *ptr = array; ptr <= pLast; ptr++)
    {
        if(*get_lowest > *ptr)
            get_lowest = ptr;
    }
    return get_lowest;
}
// Here is my sixth function definition which calculate the highest score.
int *calHighest ( int array[], int *pLast)
{
    int *get_highest = array;
    for ( int *ptr = array; ptr <= pLast; ptr++)
        {
            if ( *get_highest < *ptr)
                  get_highest=ptr;
        }
    return get_highest;
}
// Here is my seventh definition which calculate the stand deviation
double calStandDevi (int array[], int *pLast)
{
    double sum2=0;
    double avg;
    double sum_deviation=0.0;
    double stand_deviation = 0.0;
    for ( int *ptr = array; ptr <= pLast; ptr++)
        {
            sum2 += *ptr;
            avg = sum2/SIZE;
            sum_deviation += pow((*ptr-avg), 2.0);
            stand_deviation = sqrt(sum_deviation/SIZE-1);
        }
    return stand_deviation;
}
// Here is my last function definition which print the result.
void printResult ( int array[], int *pLast)
{
    double avg, stand_deviation;
    int *get_lowest, *get_highest;
    cout << "The average score is " << avg << endl;
    cout << "The lowest score is " << *get_lowest << endl;
    cout << "The highest score is " << *get_highest << endl;
    cout << "The standard deviation is " << stand_deviation << endl;
    cout << endl;
    cout << "Have a great day!!!" << endl;
}

tl;dr:你的局部变量不是量子纠缠的!


在将全局变量定义移到使用这些变量的函数中之后,您继续将局部变量视为全局变量,在一个函数中写入它们,但在另一个函数中读取它们。

但是这些是不同的变量。

这里有一个特别明显的例子,你会遇到严重的问题:

void printResult ( int array[], int *pLast)
{
    double avg, stand_deviation;
    int *get_lowest, *get_highest;
    cout << "The average score is " << avg << endl;
    cout << "The lowest score is " << *get_lowest << endl;
    cout << "The highest score is " << *get_highest << endl;
    cout << "The standard deviation is " << stand_deviation << endl;
    cout << endl;
    cout << "Have a great day!!!" << endl;
}

你声明了一个未初始化的指针get_lowest和一个指针get_highest,然后立即对它们解引用,从不让它们指向任何有用的地方。

从这里的avgstand_deviation中读取也是非法的,它们也是完全未初始化且没有值的。然而,这不太可能像你看到的那样导致崩溃;通常你只能从中得到看起来"随机"的数字。

你的教授告诉你不要使用全局变量,因为他想让你考虑如何使用参数在程序中传递信息。你不能像对待全局变量一样对待局部变量,神奇地与不同作用域的其他具有相似名称的局部变量共享状态;如果可以,告诉您不要使用全局变量就没有意义了!