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

发布于 2024-10-06 01:10:17 字数 3818 浏览 3 评论 0原文

我的程序编译罚款,但在输入文件时出现“分段错误(核心转储)”错误。我没有正确处理 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";
}

My program compiles fines but upon inputting a file I get a "Segmentation fault (core dumped)" error. Am I not handling the ostream correctly?

#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";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眸 2024-10-13 01:10:17
scale == 'c' || 'C'

不做你认为它做的事。它的解析方式如下:

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

'C' 始终为 true。如果您启用了编译器警告,您应该已经注意到这一点。

(不,这不是您眼前的问题;它在 get_temps 末尾出现。但是启用警告后,您也会看到这一点。)

scale == 'c' || 'C'

does not do what you think it does. It parses like this:

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

and 'C' is always true. If you had compiler warnings enabled, you should have noticed this.

(No, this is not your immediate problem; it's up at the end of get_temps. But with warnings enabled, you would have seen that too.)

几味少女 2024-10-13 01:10:17

连接调试器并单步调试您的代码。您可能除以 0 或超出了数组末尾。至少我们需要更多信息才能提供帮助。

Attach a debugger and step through your code. It's possible you are dividing by 0 or running past the end of an array. At the very least we need more info before we can help.

香草可樂 2024-10-13 01:10:17

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

Are you compiling on a linux machine? If so, Valgrind is a great tool for diagnosing segmentation fault errors.

梦途 2024-10-13 01:10:17

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

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

What does error do? If it's not throwing an exception, then code that calls it will keep going with bogus data.

cin >> name; only reads a "word" from the standard input, not a whole line. So if there are spaces in the file name, your program won't get the correct file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文