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 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
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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
