Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The program I am writing is a temperature conversion program that is suppose to

ID: 3914687 • Letter: T

Question

The program I am writing is a temperature conversion program that is suppose to take 2 command line arguments, example: ./a.out 1314 F, one for the temperature and one for what format its suppose to be in. i have everything correct, the only issue is that we are suppose to account for if the person only enters in one command line argument and i keep getting a segmentation fault. I have tried everything i can think of, argc < 2, !argc, and so on. Here's my program

#include <iostream> // cout
#include <cstdlib> // atoi
#include <iomanip> // fixed, setprecision

using namespace std;

int main(int argc, char *argv[]) {
     float temp, degrees, F_C, F_K, C_F, C_K, K_C, K_F;
     char temp_type;

     temp = atoi(argv[1]);
     temp_type = *argv[2]; // conversion of temperature type to char

     F_C = (temp - 32) * 5/9; // fahrenheit to celsius
     F_K = (temp + 459.67) * 5/9; // fahrenheit to kelvin
     C_F = temp * 9/5 + 32; // celsius to fahrenheit
     C_K = temp + 273.15; // celsius to kelvin
     K_C = temp - 273.15; // kelvin to celsius
     K_F = temp * 9/5 - 459.67; // kelvin to fahrenheit
  
     cout << fixed << setprecision(1);
     if (argc != 3) {
          cout << "You must enter two arguments." << endl;
          return 1;
     }
     // displays converted temps of Fahrenheit to celcius and kelvin
     if (temp_type == 'F') {
          cout << temp << "F is " << F_C << "C and " << F_K << "K" << endl;
     }

     // displays converted temps of Celcius to fahrenheit to kelvin
     if (temp_type == 'C') {
          cout << temp << "C is " << C_F << "F and " << C_K << "K" << endl;
     }

     // displays converted temps of Kelvin to fahrenheit and celsius
     if (temp_type == 'K') {
          cout << temp << "K is " << K_F << "F and " << K_C << "C" << endl;
     }

     // displays fault if wrong temperature type is entered
     if ((temp_type != 'F') && (temp_type != 'C') && (temp_type != 'K')) {
          cout << temp_type << " is not valid temperature type." << endl;
     }
   
     cout << "$" << endl;

     return 0;   
}

Explanation / Answer

Try the below code for temperature conversion :

#include<iostream>

#include<cstdio>

#include<cstdlib>

#define num .55555555555555555555

using namespace std;

void setTempKelvin(double kdegree, double cdegree, double fdegree, char type)

{

if(type == 'c'){

kdegree = cdegree + 273.15;

}

else if(type == 'f ')

{

cdegree = fdegree * num - 32;

kdegree = cdegree + 273.15;

}

cout << kdegree;

}

void setTempFahrenheit(double fdegree, double cdegree, double kdegree, char type)

{

if(type == 'c'){

fdegree = cdegree * 1.8 + 32;

}

else if(type == 'k')

{

cdegree = kdegree - 273.15;

fdegree = cdegree * 1.8 +32;

}

cout << fdegree;

}

void setTempCelsius(double cdegree, double fdegree, double kdegree, char type)

{

if(type == 'f'){

cdegree = fdegree * num - 32;

}

else if(type == 'k')

{

cdegree = kdegree - 273.15;

}

cout << cdegree;

}

int main()

{

double input, fdegree = 0, kdegree = 0, cdegree = 0;

int type;

char t;

cout<<"which kind of temp would you like to enter?" << endl;

cout << "(Kelvin(1) , Fahrenheit(2) , Celsius(3) )" ;

cin >> type;

switch (type)

{

case 1: cout<< "enter your temperature:";

cin >> input; kdegree = input;

break;

case 2: cout<< "enter your temperature:";

cin >> input; fdegree = input;

break;

case 3: cout<< "enter your temperature:";

cin >> input; cdegree = input;

break;

default :

cout<< "you have entered something invalid" << endl;

return 0;

}

switch(type)

{ case 1: t ='k';

break;

case 2: t ='f';

break;

case 3: t ='c';

break;

}

cout << "your temp in Kelvin is";

if(type != 1)

{ setTempKelvin(kdegree,cdegree,fdegree,t);

}

else { cout << input ; }

cout << endl;  

cout << "your temp in Fahrenheit is";

if(type != 2)

{ setTempFahrenheit ( fdegree,cdegree,kdegree,t);

}

else { cout << input ; }

cout << endl;

cout << "your temp in Fahrenheit is";

if(type != 3)

{ setTempCelsius ( fdegree,cdegree,kdegree,t);

}

else { cout << input ; }

cout << endl;

return 0;

}