[C++] Temperature Conversion

f0ur

Newbie
Joined
Mar 6, 2019
Messages
25
Reaction score
11
Ok, so after speaking with a professor about well... my future. He suggested that I focus on one thing at a time. I have thought about this a bit and decided to focus my attention on C/C++ and get really good at those two languages. After some deliberation, I have started out with C++... Sorry if some of you don't like C++ or if I do something 'wrong'. Anyways, the first program I have coded is a simple temperature conversion from Celsius to Fahrenheit and vice versa. My first variation of the code is:

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

}

This one was actually the first method I found via Google, but with argv, it wasn't working the way I wanted it to...^^^

I am just curious about what I could have done better and such?
 
If you are learning C++ then I would embrace C++ and drop the legacy libraries. If you are working with strings, use C++ strings. C++'s standard library is made to work well with C++ and not very well with legacy C.
Once you start with C++'s standard library templates, you'll drop all the legacy C stuff.
 
If you are really serious about C++ then you should start exploring its standard library with a book like:

The C++ Standard Library - A Tutorial and Reference, 2nd Edition

I'm not sure how current the above book is, but it was very informative(to me) when I was exploring the C++ language. C++ standard library will expose you to a huge resource of data collections, algorithms, utilities, etc. I found the C++ standard library to be the single most important part of C++. BTW, the C++ standard library is huge and feature filled.
 
If you are really serious about C++ then you should start exploring its standard library with a book like:

The C++ Standard Library - A Tutorial and Reference, 2nd Edition

I'm not sure how current the above book is, but it was very informative(to me) when I was exploring the C++ language. C++ standard library will expose you to a huge resource of data collections, algorithms, utilities, etc. I found the C++ standard library to be the single most important part of C++. BTW, the C++ standard library is huge and feature filled.
After your first comment, I figured it out. I just had to assign argv[1] as the datatype string and could then compare. I will try to check out the book, but knowing how busy I am, doubt I would have time to read much. lol.
 
Reminds me of the days when I was in school learning to program hello world in javascript. Didn't make it much further :D
 
Shouldn't these return float or double? Also the usertemp should probably be float/double too.

int ftemp_conversion( int usertemp );
int ctemp_conversion( int usertemp );
 
After your first comment, I figured it out. I just had to assign argv[1] as the datatype string and could then compare. I will try to check out the book, but knowing how busy I am, doubt I would have time to read much. lol.
The only reason someone would learn C++ is to use its standard library. The C++ standard library is the only reason C++ is popular and used today.
 
Back
Top