gpt4 book ai didi

c++ - 我的程序中出现访问冲突(段错误)?

转载 作者:行者123 更新时间:2023-11-28 03:17:55 26 4
gpt4 key购买 nike

这个程序让我很头疼。无论出于何种原因,它一到达“学生 3”就会崩溃。我找不到任何它陷入的无限循环或任何其他导致它在那时崩溃的原因。非常感谢任何帮助。

#include <iostream>
#include <string>
using namespace std;

int main()
{
const int ARRAY_SIZE = 5;
const int ARRAY_SIZE_TWO = 4;
const int ARRAY_SIZE_THREE = 20;
string names[ARRAY_SIZE];
double grades[ARRAY_SIZE];
double average[ARRAY_SIZE];
double averageFinal[ARRAY_SIZE];
string letter[ARRAY_SIZE] = {"A", "B", "C", "D", "F"};
string studentLetter[ARRAY_SIZE];
int counterOne = 0;
int counterTwo = 0;
int counterThree = 0;
double temp;
while (counterOne < ARRAY_SIZE)
{
system("CLS");
cout<<"Enter the name of student number " <<(counterOne + 1) <<":"<<endl;
cin>>names[counterOne];
while(counterTwo < ARRAY_SIZE_TWO)
{
system("CLS");
cout<<"Enter the test scores for " <<names[counterOne] <<":" <<endl;
cout<<"Enter test score number " <<(counterTwo + 1) <<" for " <<names[counterOne] <<":" <<endl;
cin>>grades[counterThree];
counterTwo += 1;
counterThree += 1;
average[counterOne] += grades[counterThree];
}
counterTwo = 0;
counterOne += 1;
}
counterOne = 0;
counterTwo = 0;
counterThree = 0;
while(counterOne < ARRAY_SIZE)
{
averageFinal[counterOne] = average[counterOne] / 12;
if (averageFinal[counterOne] < 89)
{
studentLetter[counterOne] = letter[0];
}
else if (averageFinal[counterOne] < 79)
{
studentLetter[counterOne] = letter[1];
}
else if (averageFinal[counterOne] < 69)
{
studentLetter[counterOne] = letter[2];
}
else if (averageFinal[counterOne] < 59)
{
studentLetter[counterOne] = letter[3];
}
else
{
studentLetter[counterOne] = letter[4];
}
counterOne += 1;
}

while(counterTwo < ARRAY_SIZE)
{
cout<<names[counterTwo] <<":" <<endl <<"Average: " <<averageFinal <<"%" <<endl <<"Letter Grade: " <<studentLetter[counterTwo] <<endl;
counterTwo += 1;
}
system("PAUSE");
return 0;
}

最佳答案

正如 vidit 指出的那样,允许 counterThree 在读取循环中溢出 ARRAY_SIZE。您需要重置它,或稍后使用不同的计数器。一个好的做法是尽可能缩小变量的范围,不要重复使用变量(避免使用“temps”),这会导致簿记困惑。

关于c++ - 我的程序中出现访问冲突(段错误)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16268199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com