Author Topic: C++ Temperature Converter problem  (Read 1634 times)

I am trying to make a temperature converter that changes celsius to fahrenheit and fahrenheit to celsius. But I encountered a problem with the value going dipstuff crazy.

Here is the code:
Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int value;
    int FahrenheitCONVERTED;
    int celsiusCONVERTED;
    string ConvertFrom;
    string ConvertTo;

    cout << "Hello please enter the value.\n";
    cin >> value;
    FahrenheitCONVERTED = (celsiusCONVERTED*1.8)+32;
    celsiusCONVERTED = (FahrenheitCONVERTED-32)/1.8;

    cout << "What would you like to convert " << value << " from?\n";
    cin >> ConvertFrom;

    cout << "Enter what you are going to convert it to.\n";
    cin >> ConvertTo;

    if ( ConvertFrom == "Fahrenheit") {
        if ( ConvertTo == "Celsius") {
            cout << "Fahrenheit: " << value << "   to   Celsius: " << celsiusCONVERTED;
        }
    }
    else if ( ConvertFrom == "Celsius") {
        if ( ConvertTo == "Fahrenheit") {
            cout << "Celsius: " << value << "   to   Fahrenheit: " << FahrenheitCONVERTED;
        }
    }
    else {
        cout << "Invalid statement.\n";
    }
    return 0;
}


Pics of the problem:

Code: [Select]
    cout << "Hello please enter the value.\n";
    cin >> value;
    FahrenheitCONVERTED = (celsiusCONVERTED*1.8)+32;
    celsiusCONVERTED = (FahrenheitCONVERTED-32)/1.8;
Use value in the formula instead of celsiusCONVERTED and FahrenheitCONVERTED?

Code: [Select]
    cout << "Hello please enter the value.\n";
    cin >> value;
    FahrenheitCONVERTED = (celsiusCONVERTED*1.8)+32;
    celsiusCONVERTED = (FahrenheitCONVERTED-32)/1.8;
Use value in the formula instead of celsiusCONVERTED and FahrenheitCONVERTED?
Yeah, he's trying to calculate using a variable that isn't anything.

why are you nesting if statements instead of using an and operator? not necessary to nest them for something like this

also I suggest doing your logic inside the if statements instead of at the beginning of your code

you should probably use single/double instead of integer to keep precision .. but then again when was the last time the weatherman said "it'll be 27.5 degrees outside today.."
« Last Edit: January 13, 2012, 06:35:41 PM by SpreadsPlague »

why are you nesting if statements instead of using an and operator? not necessary to nest them for something like this

also I suggest doing your logic inside the if statements instead of at the beginning of your code

I am not familiar with C++ much and didn't know what the and variable was.

EDIT: Tammy's logic didn't work.


I am not familiar with C++ much and didn't know what the and variable was.

EDIT: Tammy's logic didn't work.
You're storing information in variables that have nothing in them.

Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int value;
double converted;
string convert;

    cout << "Hello please enter the value.\n";
    cin >> value;

    cout << "What would you like to convert " << value << " to?\n";
    cin >> convert;

    if ( convert == "C") {
converted = (value-32)/1.8;
        cout << "Fahrenheit: " << value << "   to   Celsius: " << converted;
    }
    else if ( convert == "F") {
converted = (value*1.8)+32;
        cout << "Celcius: " << value << "   to   Fahrenheit: " << converted;
    }
    else {
        cout << "Invalid statement.\n";
    }
    return 0;
}

i have no idea if this works or not

You're storing information in variables that have nothing in them.

no, he's using an empty variable in a formula. it doesn't matter if he stores something into an empty variable, that fills it.

no, he's using an empty variable in a formula. it doesn't matter if he stores something into an empty variable, that fills it.
Sorry, that's what I meant.

Updated code:
Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int StartValue;
    int value;
    int celsiusCONVERTED;
    int FahrenheitCONVERTED;
    string ConvertFrom;
    string ConvertTo;

    cout << "Hello please enter the value.\n";
    cin >> StartValue;

    cout << "What would you like to convert " << StartValue << " from?\n";
    cin >> ConvertFrom;

    cout << "Enter what you are going to convert it to.\n";
    cin >> ConvertTo;

    if ( ConvertFrom == "Fahrenheit") {
        if ( ConvertTo == "Celsius") {
            FahrenheitCONVERTED = (value*1.8)+32;
            cout << "Fahrenheit: " << StartValue << "   to   Celsius: " << value;
        }
    }
    else if ( ConvertFrom == "Celsius") {
        if ( ConvertTo == "Fahrenheit") {
            celsiusCONVERTED = (value-32)/1.8;
            cout << "Celsius: " << StartValue << "   to   Fahrenheit: " << value;
        }
    }
    else {
        cout << "Invalid statement.\n";
    }
    return 0;
}
Still makes the fahrenheit number too high.

it should be
cout << "Fahrenheit: " << StartValue << " to Celsius " << FahrenheitCONVERTED;

and
cout << "Celsius: " << StartValue << " to Fahrenheit " << CelsiusCONVERTED;
i think

you're STILL using an empty variable in your formula.

by the way you're declaring tons of variables you don't need. rewrite your code using only these (names don't matter but they're straightforward):


int inputNum
int answer
string input


edit: okay so I did it for you but whatever
« Last Edit: January 13, 2012, 06:52:05 PM by SpreadsPlague »

in your formula, it now uses "value" which you never even declared.

Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    int inputNum = 0;
    int answer = 0;
    string input = "";

    cout << "Hello, please enter the value.\n";
    cin >> inputNum;

    cout << "Is that in Fahrenheit or Celsius?\n";
    cin >> input;

    if (input == "Fahrenheit")
   {
            answer = (inputNum-32)*(5/9);
            cout << "Fahrenheit: " << inputNum << "   to   Celsius: " << answer;
    }
    else if (input == "Celsius")
   {
            answer = (inputNum*(9/5))+32;
            cout << "Celsius: " << inputNum << "   to   Fahrenheit: " << answer;
    }
    else
   {
        cout << "Invalid statement.\n";
    }
    return 0;
}

this will work
my only question is .. why does main have a return type int?
« Last Edit: January 13, 2012, 07:00:16 PM by SpreadsPlague »