exe运行后停止工作
program.exe has stopped working after running it
我不知道我有什么问题。我构建没有错误,一切都很好。老实说,我得到了这个项目。然而,我的教授不允许使用全局变量。因此,我将变量放入main函数和其他void函数中。然而,当我运行它时,它说"program.exe已停止工作"。我不知道该怎么补救。我可能在某个地方用错了指针,或者我可能计算错了标准差。我不知道。任何帮助都将是感激的。非常感谢!!这是我的输入文件"scores.txt":
4685 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
,然后立即对它们解引用,从不让它们指向任何有用的地方。
从这里的avg
和stand_deviation
中读取也是非法的,它们也是完全未初始化且没有值的。然而,这不太可能像你看到的那样导致崩溃;通常你只能从中得到看起来"随机"的数字。
你的教授告诉你不要使用全局变量,因为他想让你考虑如何使用参数在程序中传递信息。你不能像对待全局变量一样对待局部变量,神奇地与不同作用域的其他具有相似名称的局部变量共享状态;如果可以,告诉您不要使用全局变量就没有意义了!
相关文章:
- Visual Studio 2017 停止工作,并在打开后显示许多控制台窗口
- 自 Windows 10 20H1 以来,具有单独线程的多个窗口停止工作
- C++代码停止工作错误使用cout内部函数
- LoadImage 在 Windows 更新后停止工作
- C++程序工作,但 Windows 显示"程序.exe已停止工作"
- VS 代码 C/C++ 智能感知在更新后停止工作
- 不使用与左右停止工作命名空间 std 的简单比较
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- 德拉吉诺 LG01-S 收到异常数据包并停止工作
- G++ 在生成文件中使用时停止工作
- 当循环在条件C++之前停止工作时
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- Vscode 自动格式化代码在运行几次后停止工作
- 运行时停止工作
- 每当我尝试运行此代码时,断裂出现或停止工作
- 当运行程序时,它说程序.exe停止工作
- 导致运行反向字符串程序时停止工作
- cvLoadimage 在运行时停止工作
- 代码中没有错误,但是当我运行程序时,我得到“多项式.exe已停止工作”.错误
- exe运行后停止工作