分段错误(核心转储)错误

时间:2010-12-06 04:09:25

标签: c++ fault segmentation-fault ostream

我的程序编译罚款,但在输入文件后,我收到“分段错误(核心转储)”错误。我没有正确处理ostream吗?

#include <std_lib_facilities.h>

struct Reading {
    int hour;
    double temperature;
    Reading(int h, double t): hour(h), temperature(t) { }
    bool operator<(const Reading &r) const;
};

bool Reading::operator<(const Reading &r) const
{
// stub version                                                                                         

    if (temperature < r.temperature){

        return true;

}
    else if (r.temperature < temperature)  {

        return false;
    }


}

/*                                                                                                      
 * function declarations                                                                                
 */

ostream& operator<<(ostream& ost, const Reading &r);

vector<Reading> get_temps();

double check_adjust_temp(double temperature, char scale);

double c_to_f(double temperature);

double mean(vector<Reading> temps);

double median(vector<Reading> temps);

void print_results(const vector<Reading>& temps, double mean_temp,
                   double median_temp);

int main()
    try
        {
            vector<Reading> temps = get_temps();
            if (temps.size() == 0) error("no temperatures given!");
            double mean_temp = mean(temps);
            sort(temps.begin(), temps.end());
            double median_temp = median(temps);
            print_results(temps, mean_temp, median_temp);
        }
    catch (exception& e) {
        cerr << "error: " << e.what() << '\n';
        return 1;
    }
    catch (...) {
        cerr << "Oops: unknown exception!\n";
        return 2;
    }

/*                                                                                                      
 * function definitions                                                                                 
 */

ostream& operator<<(ostream& ost, const Reading &r)
{

    return ost << '(' << r.hour
               << ',' << r.temperature <<')';
}

vector<Reading> get_temps()
{
    cout << "Please enter name of input file name: ";
    string name;;
    cin >> name;
    ifstream ist(name.c_str());
    if(!ist) error("can't open input file ", name);

    vector<Reading> temps;
    int hour;
    double temperature;
    while (ist >> hour >> temperature){
        if (hour <0 || 23 <hour) error("hour out of range");
        temps.push_back( Reading(hour,temperature));
    }

}

double check_adjust_temp(double temperature, char scale)
{
    if (scale == 'c' || 'C'){

        return c_to_f(temperature);
    }
    else if (scale == 'f' || 'F')  {

        return temperature;
    }
    else {

        error("Wrong input type");
    }
}

double c_to_f(double temperature)
{
    double c;
    c = ((temperature * (9.0/5)) + 32);
    return (c);
}

double mean(vector<Reading> temps)
{
    double mean_temp;
    double sum = 0;
    for (int i = 0; i< temps.size(); ++i) sum += temps[i].temperature;
    mean_temp = sum/temps.size();
    return (mean_temp);
}

double median(vector<Reading> temps)
{
    double median_temp;
    sort (temps.begin(), temps.end());
    median_temp = temps[temps.size()/2].temperature;
    return (median_temp);
}

void print_results(const vector<Reading>& temps, double mean_temp,
                   double median_temp)
{

    cout << "The sorted temperatures are:\n";
    cout << get_temps;
    cout << "The mean temperature is " << mean_temp << ".\n";
    cout << "The median temperature is " << median_temp << ".\n";
}

4 个答案:

答案 0 :(得分:4)

scale == 'c' || 'C'

不符合您的想法。它解析如下:

( scale == 'c' ) || 'C'

并且'C'始终为真。如果你启用了编译器警告,你应该注意到这一点。

(不,这不是你的直接问题;它会在get_temps结束时出现。但是如果启用了警告,你也会看到它。)

答案 1 :(得分:0)

附加调试器并逐步执行代码。你有可能将0除以或超过数组的末尾。在我们提供帮助之前,我们至少需要更多信息。

答案 2 :(得分:0)

你在linux机器上编译吗?如果是这样,Valgrind是诊断分段错误错误的绝佳工具。

答案 3 :(得分:0)

error做什么?如果它没有抛出异常,那么调用它的代码将继续使用虚假数据。

cin >> name;只从标准输入中读取“单词”,而不是整行。因此,如果文件名中有空格,则程序将无法获得正确的文件名。