Support Forums

Full Version: Celsius and Farenheit Converter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my first C++ program and all it does is convert Fahrenheit to Celsius and vise versa, tell me what you guys think or if there is anything I can improve on.
Code:
/*
This is my first C++ program,
it converts C to F and vise versa,
made by uber1337
*/

#include <iostream>
using namespace std;

int main ()
{
    int choice;
    cout << "1.) Celsius to Fahrenheit" << endl;
    cout << "2.) Fahrenheit to Celsius" << endl;
    cout << "What would you like to do?:" << endl;
    cin >> choice;
    if (choice == 1)
    {
        float celsius, fahrenheit;
        cout << "Enter degrees in Celsius:" << endl;
        cin >> celsius;
        fahrenheit = ((celsius * 9.0) / 5.0) + 32.0;
        cout << celsius << " Degrees Celsius is equal to " << fahrenheit << " degrees Fahrenheit" << endl;
        cin >> celsius; //So the windows doesn't disapear
    }              
    else if (choice == 2)
    {
        float celsius, fahrenheit;
        cout << "Enter degrees in Fahrenheit:" << endl;
        cin >> fahrenheit;
        celsius = ((fahrenheit - 32) / 9) * 5;
        cout << fahrenheit << " Degrees Fahrenheit is equal to " << celsius << " degrees Celsius" << endl;
        cin >> celsius; //So the windows doesn't disapear
    }
    else
    {
        cout << "You have entered an incorrect option";
    }
}

Blackhat
(03-30-2010, 08:58 AM)uber1337 Wrote: [ -> ]This is my first C++ program and all it does is convert Fahrenheit to Celsius and vise versa, tell me what you guys think or if there is anything I can improve on.
Code:
/*
This is my first C++ program,
it converts C to F and vise versa,
made by uber1337
*/

#include <iostream>
using namespace std;

int main ()
{
    int choice;
    cout << "1.) Celsius to Fahrenheit" << endl;
    cout << "2.) Fahrenheit to Celsius" << endl;
    cout << "What would you like to do?:" << endl;
    cin >> choice;
    if (choice == 1)
    {
        float celsius, fahrenheit;
        cout << "Enter degrees in Celsius:" << endl;
        cin >> celsius;
>>>     fahrenheit = ((celsius * 9.0) / 5.0) + 32.0;
        cout << celsius << " Degrees Celsius is equal to " << fahrenheit << " degrees Fahrenheit" << endl;
        cin >> celsius; //So the windows doesn't disapear
    }              
    else if (choice == 2)
    {
        float celsius, fahrenheit;
        cout << "Enter degrees in Fahrenheit:" << endl;
        cin >> fahrenheit;
        celsius = ((fahrenheit - 32) / 9) * 5;
        cout << fahrenheit << " Degrees Fahrenheit is equal to " << celsius << " degrees Celsius" << endl;
        cin >> celsius; //So the windows doesn't disapear
    }
    else
    {
        cout << "You have entered an incorrect option";
    }
}

Blackhat
I'm not familiar with C++, but I can safely say you have left out a ')' in line 22.
Hah this would help me at my Physic test xD
(03-30-2010, 02:25 PM)Μαύρο Wrote: [ -> ]I'm not familiar with C++, but I can safely say you have left out a ')' in line 22.
Heh maybe you should take another look.
Code:
fahrenheit = ((celsius * 9.0) / 5.0) + 32.0;
Aye, bad mistake. Confusing looking at code gray-gray.
On windows you can use _getch(); for the "window not disappearing" effect.
Never knew those formulas to convert Thanks allot .
Nice code bro Smile thanks tons