Code:
#include <iostream>
using namespace std;
int ftemp_conversion( int ftemp );
int ctemp_conversion( int ctemp );
int main(void){
// Create variables ftemp and ctemp as ints
int ftemp, ctemp;
// Ask for ftemp from user
cout << "Enter in a temperature in fahrenheit: ";
cin >> ftemp;
// Call function ftemp_conversion
ctemp = ftemp_conversion( ftemp );
cout << "The temperture in celsius is: " << ctemp << endl;
// Zero out both ctemp and ftemp
ctemp = 0;
ftemp = 0;
// Ask for ctemp from user
cout << "Enter in a temperature in celsius: ";
cin >> ctemp;
ftemp = ctemp_conversion( ctemp );
cout << "The temperature in fahrenheit is: " << ftemp << endl;
return 0;
}
int ftemp_conversion( int ftemp ){
int final_temp;
// The conversion
final_temp = (ftemp - 32) * 5 / 9;
return final_temp;
}
int ctemp_conversion( int ctemp ){
int final_temp;
// The conversion
final_temp = (ctemp * 9 / 5) + 32;
return final_temp;
}
Obviously not ideal 100% if lets say, the user wants to input command line arguments and etc. and so I took it one step further and added command line args. Code is here:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int ftemp_conversion( int usertemp );
int ctemp_conversion( int usertemp );
void banner();
int main( int argc, char *argv[] ){
int ftemp, ctemp;
if( argc < 2 ){
/*
* Usage messages are convenient ways to
* tell user how to run the program.
*/
cout << "[!] Usage: " << argv[0] << " <conversion>" << endl;
return 1;
}
banner();
// Compare argv[1] to see which conversion to use
if(strcmp(argv[1], "ctemp") == 0){
cout << "Enter temperature in fahrenheit: ";
cin >> ftemp;
// Call ftemp_conversion and return result
// that gets assigned to ctemp
ctemp = ftemp_conversion(ftemp);
cout << "The temperature in celsius is: " << ctemp << endl;
}
else if(strcmp(argv[1], "ftemp") == 0){
cout << "Enter temperature in celsius: ";
cin >> ctemp;
// Call ctemp_conversion and return result
// that gets assigned to ctemp
ftemp = ctemp_conversion(ctemp);
cout << "The temperature in fahrenheit is: " << ftemp << endl;
}
else{
cout << "Enter in a valid conversion" << endl;
return 1;
}
return 0;
}
void banner(){
cout << "This program is to convert Fahrenheit to Celisus " << endl;
cout << "and from Celisus to Fahrenheit. " << endl << endl;
}
int ftemp_conversion( int usertemp ){
int final_temp;
final_temp = ( usertemp - 32) * 5 / 9;
return final_temp;
}
int ctemp_conversion( int usertemp ){
int final_temp;
final_temp = ( usertemp * 9 / 5) + 32;
return final_temp;
}
The only 'real issue' I ran into is that for some stupid reason, C++ has in my opinion, way too many ways of comparing two strings together. Coming a bit from C, I decided on strcmp, but I am curious if there might be a better way of doing it? I know that string header file is an option and do something like:
Code:
std::string str1;
std::string str2;
if(str1.compare(str2) == 0){
//do stuff
}
#include <iostream>
using namespace std;
int ftemp_conversion( int ftemp );
int ctemp_conversion( int ctemp );
int main(void){
// Create variables ftemp and ctemp as ints
int ftemp, ctemp;
// Ask for ftemp from user
cout << "Enter in a temperature in fahrenheit: ";
cin >> ftemp;
// Call function ftemp_conversion
ctemp = ftemp_conversion( ftemp );
cout << "The temperture in celsius is: " << ctemp << endl;
// Zero out both ctemp and ftemp
ctemp = 0;
ftemp = 0;
// Ask for ctemp from user
cout << "Enter in a temperature in celsius: ";
cin >> ctemp;
ftemp = ctemp_conversion( ctemp );
cout << "The temperature in fahrenheit is: " << ftemp << endl;
return 0;
}
int ftemp_conversion( int ftemp ){
int final_temp;
// The conversion
final_temp = (ftemp - 32) * 5 / 9;
return final_temp;
}
int ctemp_conversion( int ctemp ){
int final_temp;
// The conversion
final_temp = (ctemp * 9 / 5) + 32;
return final_temp;
}
Obviously not ideal 100% if lets say, the user wants to input command line arguments and etc. and so I took it one step further and added command line args. Code is here:
Code:
#include <iostream>
#include <cstring>
using namespace std;
int ftemp_conversion( int usertemp );
int ctemp_conversion( int usertemp );
void banner();
int main( int argc, char *argv[] ){
int ftemp, ctemp;
if( argc < 2 ){
/*
* Usage messages are convenient ways to
* tell user how to run the program.
*/
cout << "[!] Usage: " << argv[0] << " <conversion>" << endl;
return 1;
}
banner();
// Compare argv[1] to see which conversion to use
if(strcmp(argv[1], "ctemp") == 0){
cout << "Enter temperature in fahrenheit: ";
cin >> ftemp;
// Call ftemp_conversion and return result
// that gets assigned to ctemp
ctemp = ftemp_conversion(ftemp);
cout << "The temperature in celsius is: " << ctemp << endl;
}
else if(strcmp(argv[1], "ftemp") == 0){
cout << "Enter temperature in celsius: ";
cin >> ctemp;
// Call ctemp_conversion and return result
// that gets assigned to ctemp
ftemp = ctemp_conversion(ctemp);
cout << "The temperature in fahrenheit is: " << ftemp << endl;
}
else{
cout << "Enter in a valid conversion" << endl;
return 1;
}
return 0;
}
void banner(){
cout << "This program is to convert Fahrenheit to Celisus " << endl;
cout << "and from Celisus to Fahrenheit. " << endl << endl;
}
int ftemp_conversion( int usertemp ){
int final_temp;
final_temp = ( usertemp - 32) * 5 / 9;
return final_temp;
}
int ctemp_conversion( int usertemp ){
int final_temp;
final_temp = ( usertemp * 9 / 5) + 32;
return final_temp;
}
The only 'real issue' I ran into is that for some stupid reason, C++ has in my opinion, way too many ways of comparing two strings together. Coming a bit from C, I decided on strcmp, but I am curious if there might be a better way of doing it? I know that string header file is an option and do something like:
Code:
std::string str1;
std::string str2;
if(str1.compare(str2) == 0){
//do stuff
}