【问题标题】:C++ Broken Program - WPRFLAG?C++ 损坏的程序 - WPRFLAG?
【发布时间】:2015-06-17 21:45:03
【问题描述】:

我正在为班级创建一个程序,我们应该让它获取一个成绩列表(最多 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);

}

只是想感谢您为此付出的任何时间。非常感谢所有回复!如果我找到答案,我会尝试更新这个。

【问题讨论】:

  • 您确定这种情况:counter&lt;numberGrades+1?我怀疑你的阅读越界了。
  • 尝试跨步而不是跨步,尤其是在使用标准库函数时。
  • 没有真正查看您的代码,我将其复制并粘贴到我的 Visual Studio 环境中,它看起来好像工作正常。有什么具体的输入可以重现问题吗?
  • 这里也一样。用 g++ 试过。没看代码。似乎工作。 (我没有检查计算结果是否正确)
  • 对于输入,我输入 1-100 作为前 100 个等级或 1-20 (1...2...3..4...55..56..99. .100 或 1..2..3..19..20..exit)。没想到回复这么快。谢谢!

标签: c++ error-handling


【解决方案1】:

将以下几行移出主函数中的最终 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多