gpt4 book ai didi

C++ 损坏的程序 - WPRFLAG?

转载 作者:太空宇宙 更新时间:2023-11-04 13:35:59 24 4
gpt4 key购买 nike

我正在为类(class)创建一个程序,我们应该让它获取成绩列表(最多 100 个成绩),对它们进行排序,然后输出平均值和中位数以及 5 分的成绩列表每行的成绩。我的代码已完成,它构建并开始运行,但退出时没有任何输出。单步执行以查找错误,它会跳转到另一个名为 crtexe.c 的文件中的 WPRFLAG 行。我想发布我的代码,看看是否有人可以帮助我指明正确的方向。现在,我不知道这个 wprflag 是什么,也不明白为什么我的代码刚刚退出。

#include <iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<sstream>
using namespace std;
inline void keep_window_open() { char ch; cin>>ch; }
///////////////////////////////Function Prototypes///////////////////////////////
double mean (int[], int);
double median (int[], int);
///////////////////////////////Main//////////////////////////////////////////////
//Main takes input for up to 100 grades and stores in an array then calls the
//mean and median functions for those calculations then outputs the mean, median,
//and list of grades sorted low to high with 5 grades per line
//Main takes input in as a string and coverts to int. This is to allow user to
//enter EXIT to end the input loop. There is some error checking built in so
//use can only enter 0-100 or exit.
int main ()
{
int counter=0, valueInt=0, numberGrades=0;
//counter is used to limit loop to 100 cycles and indicate which element in
//array to store valueInt
int grades [100] = {0};
string valueString=" ";
cout<<"Use this program to calculate the Mean and Median of up to 100 ";
cout<<"grades.\n**Please enter positive real whole numbers only ";
cout<<"(0-100).\n";
do //main input loop
{
cout<<"Key in a grade and press [Enter] or type EXIT to end list\n";
cin>>valueString;
if (valueString=="exit"||valueString=="EXIT"||valueString=="Exit")
{
break;
}
else
{
stringstream convert(valueString); //converts String to Int
if (!(convert>>valueInt)) //for array
{
valueInt=0;
}

if (cin.fail()) //error checking for any
{ //text input that is not
cin.clear(); //"exit"
cin.ignore(1000,'\n');
cout<<"Incorrect input. Try again\n";
break;
}
if (valueInt<0||valueInt>100) //error checking for
{ //0>valueInt>100
cout<<"Grades can only be 0-100. Please try again.\n";
break;
}
else
{
grades[counter]=valueInt; //Actual input into array
numberGrades++;
counter++;
}
}
}
while (counter<100);
cout<<"The average grade point is "<<mean(grades, numberGrades)<<".\n";
cout <<"The median grade point is "<<median(grades, numberGrades)<<".\n";

for (int outer=0;outer<numberGrades;outer++) //sorting loop
{
for (int inner=0;inner<numberGrades-1;inner++)
{
if (grades[inner]>grades[inner+1])
{
swap(grades[inner], grades[inner+1]);
}
}
}

counter=0;
for (int i=0;i<counter;i++) //print list loop
{
cout<<grades[counter]<<" ";
counter++;
if (i==4)
{
cout<<"\n";
}



//The next two lines stop the Command Prompt window from
//closing until the user presses a key, including Enter.
cout << "\nPress enter twice to exit." << endl;
cin.ignore(2);
}
}
///////////////////////////////Mean//////////////////////////////////////////////
//Mean() finds the averge of the list of grades stored in the grades array and
//returns that value to the cout statement in main().
double mean (int grades[], int numberGrades)
{
double total=0.0;
for (int counter=0;counter<numberGrades+1;counter++)
{
total+=grades[counter];
}
total=total/numberGrades;
return (total);
}

///////////////////////////////Median////////////////////////////////////////////
//Median() first sorts the values stored in the grades array then finds the
//median value. If numberGrades is odd value, will show the middle value of the
//sorted array list. If numberGrades is an even value, will display the average
//of the two middle values in the sorted array list.
double median (int grades[], int numberGrades)
{
double total=0.0;
for (int outer=0;outer<numberGrades;outer++) //sorting loop
{
for (int inner=0;inner<numberGrades-1;inner++)
{
if (grades[inner]>grades[inner+1])
{
swap(grades[inner], grades[inner+1]);
}
}
}

if (numberGrades%2==0)
{
total=((grades[numberGrades/2]+grades[numberGrades/2-1])/2);
}
else
{
total=grades[numberGrades/2];
}
return (total);

}

只是想对您投入的时间表示感谢。非常感谢所有回复!如果我找到答案,我会尝试更新它。

最佳答案

将以下行从主函数的最终 for 循环中移出。这让控制台保持打开状态以显示输出。

程序按编码正常工作。由于逻辑错误,我的输出不正确。

//The next two lines stop the Command Prompt window from 
//closing until the user presses a key, including Enter.
cout << "\nPress enter twice to exit." << endl;
cin.ignore(2);

关于C++ 损坏的程序 - WPRFLAG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590803/

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