Segmentation fault (core dumped) Error(分段错误(核心转储)错误)
问题描述
我的程序编译错误,但在输入文件时出现分段错误(核心转储)"错误.我没有正确处理 ostream 吗?
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() << '
';
return 1;
}
catch (...) {
cerr << "Oops: unknown exception!
";
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:
";
cout << get_temps;
cout << "The mean temperature is " << mean_temp << ".
";
cout << "The median temperature is " << median_temp << ".
";
}
推荐答案
scale == 'c' || 'C'
不做你认为它做的事.解析如下:
does not do what you think it does. It parses like this:
( scale == 'c' ) || 'C'
和 'C'
始终为真.如果您启用了编译器警告,您应该已经注意到这一点.
and 'C'
is always true. If you had compiler warnings enabled, you should have noticed this.
(不,这不是您的直接问题;它出现在 get_temps
的末尾.但启用警告后,您也会看到.)
(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.)
这篇关于分段错误(核心转储)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:分段错误(核心转储)错误
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07