I've made my first little program written in C++ and was wondering if anyone could evaluate it and suggest some improvements. No copy paste. I was wondering if I can do something with my little knowledge and came up with the simple idea of converting Celsius and Fahrenheit.

I don't know why, but when using 9/5, it gives a different output than when using 1.8 or 9.0/5.0.

#include <iostream>
using namespace std;

// My very first code written by myself

  char temperature; // what are we converting ( celsius or farenheit into another )
  float value; // value that is being converted ( -10, 0, 10, 20.5 etc. )

int main()
    cout << "Welcome" << "\n" << "This little program converts Celsius to Fahrenheit and vice versa" << endl;
    cout << "Enter c for Celsius, f for Fahrenheit or p for proof" << endl;
    cin >> temperature; // storing input which accepts only c, f, p
    switch (temperature) { // checking input for one of 3 accepted options, else using default
        case 'c' : {
            cout << "Converting Celsius to Fahrenheit" << "\n" << "Enter value" << endl;
            cin >> value; // storing the value of temperature
            float farenheit = value*1.8+32; // converting celsius to fahrenheit using the formula
            cout << value << " Celsius in Fahrenheit is: " << farenheit << endl;
        case 'f' : {
            cout << "Converting Fahrenheit to Celsius" << "\n" << "Enter value" << endl;
            cin >> value; // storing the value of temperature
            float celsius = (value-32)/1.8; // converting fahrenheit to celsius using the formula
            cout << value << " Fahrenheit in Celsius is: " << celsius << endl;
        case 'p' : {
            cout << "Proof that program is running correctly, here is a list of -20 to 20 Celsius converted to Fahrenheit" << endl;
            for (int i=-20; i<21; i++) { // using a for loop to print Celsius to Fahrenheit values from -20 to 20
                cout << i << " = " << i*1.8+32 << endl; // output Celsius = Fahrenheit
            cout << "Invalid input! Use c, f or p" << endl; // any input except the 3 accepted options will result in this default output
    cout << "Made by blackborg";
    return 0;

1 Answer 1

  • You might as well break the habit of using using namespace std. It involves a little more additional code, but it'll be helpful in the future, especially when using header files.

  • You don't need those global variables outside of main(). You should really avoid global variables unless absolutely necessary since they can be changed from any scope and you may not even know it. Just put those two variables inside if main() before their use.

  • farenheit is spelled incorrectly; it should be fahrenheit.

  • What if the user uses an uppercase 'C' or 'F' instead? You should account for those as well:

    case 'c':
         'C': {

    case 'f':
         'F': {
  • It may be more readable to multiply by both 9.0/5.0 and 5.0/9.0 respectively instead of 1.8 since that's most common with these conversion formulas. Make sure not to use integers for this, otherwise the division will truncate towards 0 (in this case, you'll end up with 1 instead of 1.8 and 0 instead of 0.555556).

  • You may find it better to use double instead of float in most cases and because the former has more precision.

  • For the next step up, I would recommend adding additional functions since main() shouldn't have to do all of the work.

    You can simply acquire value from main(), pass it to the applicable function, then return the result and display it. It'll also allow you to avoid duplicating code in the switch, such as having multiple inputs for value, which is very nice and will be essential in every other program you create. I'll let you come up with that yourself.

  • If you want to make your program more useful, you can also add Kelvins.

  • You should probably put that tagline cout in a comment instead, otherwise it may be annoying to see it come up each time when only a result is expected to be printed.

  • You don't really need return 0 at the end of main() since success is already guaranteed at that point, though it won't hurt anything either.

  • 2
    \$\begingroup\$ But ve aware of the difference between 9/5 and 9.0/5.0! \$\endgroup\$ Commented Mar 24, 2018 at 14:43
  • \$\begingroup\$ Thanks for very informative response. I'll update my code when i'm done with all your suggestions. P.S. thanks for pointing out the spelling, i first typed it wrong, then started typing correctly and fixing the code, but must have missed somewhere. P.P.S. Still wondering, if the code looks readable and is alright for a first time program? If it can even be called a program.. not sure. \$\endgroup\$
    – blackborg
    Commented Mar 24, 2018 at 19:48
  • 1
    \$\begingroup\$ @blackborg: I think it looks okay for starting out. If anything, your formatting is very clean, such as your semicolons not being scattered all over the place. As for editing your question, it would be best to post a follow-up question with the improvements. Code in questions cannot be edited after an answer has been posted. \$\endgroup\$
    – Jamal
    Commented Mar 24, 2018 at 21:06
  • \$\begingroup\$ made a follow-up: codereview.stackexchange.com/questions/191098/… \$\endgroup\$
    – blackborg
    Commented Apr 2, 2018 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.