0

I have a CompSci 1 assignment, that wants me to calculate and output the windchill factor using functions, and inputs from user. I have my functions correct, I believe, but the program is running twice. When I input values in, when it runs the second time I get:

Signal: segmentation fault (core dumped)

How do I get my program to output the Windchill factor and not run again?

#include <iostream>
#include <cmath>
using namespace std;

double ctof(double tempature){
  double farenheit = 9/5 * tempature + 32;
  farenheit = tempature;
}

double ftoc(double tempature){
  double celsius = 5/9 * (tempature - 32);
  celsius = tempature;
}

double windchill(double tempature, double wind_speed, char scale){
  switch(scale){
    case 'C' :tempature = ctof(tempature);
    break;
  }
  double W = 35.74+(0.6215 * tempature) - 35.75 * pow(wind_speed, 0.16) + 0.4275 * tempature * pow(wind_speed, 0.16);
  switch(scale){
    case 'C':
      W = ftoc(W);
      cout << "The windchill factor is " << W << "C degrees";
      break;  
    case 'F': 
      cout << "The windchill factor is " << W << "F degrees";
      break;
  }
}

int main() {
  double tempature;
  char scale;
  double wind_speed;
  cout << "Input tempature including scale (Farenhit or Celcius): ";
  cin >> tempature >> scale;
  cout << "Input wind speed: ";
  cin >> wind_speed;
  windchill(tempature, wind_speed, scale);
}

output:

Input tempature including scale (Farenhit or Celcius): 107 F
Input wind speed: 20
The windchill factor is 118.378F degrees
Input tempature including scale (Farenhit or Celcius): 107 F
Input wind speed: 20
signal: segmentation fault (core dumped)
Ken White
  • 123,280
  • 14
  • 225
  • 444
ArinJG
  • 1
  • 8
    You promise to `return` a `double` from `ftoc`, `ctof` and `windchill`, but you don't do that. [Turn on your compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) to let compiler tell you about the problem before you run the program and it crashes. – Yksisarvinen Mar 25 '22 at 23:23
  • 3
    Your program code does not run twice, but it does have *undefined behavior* in a few spots. In `ctof()`, change `farenheit = tempature;` to `return farenheit;`. In `ftoc()`, change `celsius = tempature;` to `return celsius;` And change the return type of `windchill()` from `double` to `void`. – Remy Lebeau Mar 25 '22 at 23:24
  • 1
    There's nothing in your code that would explain it appearing to run twice - the cause of that is likely to be in how you've run the program (which you haven't shown). Undefined behaviour like your code has (and you need to correct) is unlikely to cause that. You'll also find (once the undefined behaviour is corrected) that `double celsius = 5/9 * (tempature - 32);` always produces a result of zero, which is (presumably) not your intent. `5` and `9` are both `int` literals, so dividing them produces an `int` with rounding toward zero. Also, you've misspelt `temperature` – Peter Mar 26 '22 at 00:10
  • 1
    @Peter `main` running multiple times is a common symptom of missing a return from a function. The compiler will optimize away the `ret` statement since that must be unreachable and whatever function is placed after will just continue executing. You can easily cause that behavior at least with Clang on godbolt. – user17732522 Mar 26 '22 at 00:23
  • Compile with warnings, or stop ignoring them – Taekahn Mar 26 '22 at 02:34

0 Answers0