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

Your code gives the wrong answer Speadsplague, I am pretty sure 37 Celsius is not 32 Fahrenheit.

okay fixed. you had the formulas mixed up to begin with by the way. try it now.
« Last Edit: January 13, 2012, 06:56:48 PM by SpreadsPlague »

this will work
my only question is .. why does main have a return type int?
Because it's main. Why are you even asking?

-snip-
im no C++ expert but value doesnt seem to even exist anymore and you're still using it

im no C++ expert but value doesnt seem to even exist anymore and you're still using it

I had to filter that out.

Also, the conversion is wrong. Even after I flipped the formula like Spead said.

Because it's main. Why are you even asking?

I've never actually coded in C++, my background is Java. Seems strange to me.

im no C++ expert but value doesnt seem to even exist anymore and you're still using it

correct. wasn't a rewrite, I just edited his code and didn't notice I left that in there. the code is fixed now.


okay NOW it's fixed. holy stuff give me an IDE, writing code in plaintext format is a pain lol
« Last Edit: January 13, 2012, 07:00:40 PM by SpreadsPlague »

okay NOW it's fixed. holy stuff give me an IDE, writing code in plaintext format is a pain lol
notepad++?

also, this is so much easier in lua lol.

Code: [Select]
while true do
print 'conversion? [temperature] to [C/F]'
input = io.read()

if input=='exit' then break end

space = string.find(input,' ')

if space then
v = tonumber(string.sub(input,1,space-1))
cmd = string.lower(string.sub(input,space+1,string.len(input)))
if v and cmd then
io.write('Result: ')
if cmd=='f' then
io.write(math.floor((v*1.8)+32))
elseif cmd=='c' then
io.write(math.floor((v-32)/1.8))
else
print 'Invalid Input'
end
print ''
end
else
print 'Invalid Input'
end
end

also if you're using string shouldn't you include <string.h>?

okay NOW it's fixed. holy stuff give me an IDE, writing code in plaintext format is a pain lol
I am using Code::Blocks, google it and download the 70 mb one, it comes with compilers.

this is giving me a forgetton of errors involving operands in an actual IDE

OP, are you sure your syntax is correct? I'm not familiar with C++ so I couldn't tell you. My code is assuming you know the basics of what you're doing and I'm following it.

this is giving me a forgetton of errors involving operands in an actual IDE

OP, are you sure your syntax is correct? I'm not familiar with C++ so I couldn't tell you. My code is assuming you know the basics of what you're doing and I'm following it.

There are no syntax errors. The problem at the moment is that the conversion isn't accurate.


Code: [Select]

#include <iostream>
#include <string.h>

using namespace std;

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

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

    cout << "What are you converting to?\n";
    cin >> input;

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


no idea why it doesn't work. I'm not familiar enough with C++

anyway this is as far as I got:
Code: [Select]
#include <iostream>
#include <string>
using namespace std;

int main()
{
    double inputNum = 0;
    double 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.compare("Fahrenheit"))
   {
        answer = (inputNum-32)*(5/9);
        cout << "Fahrenheit: " << inputNum << " to Celsius: " << (int)answer;
   }
   else if(input.compare("Celsius"))
   {
        answer = (inputNum*(9/5))+32;
        cout << "Celsius: " << inputNum << " to Fahrenheit: " << (int)answer;
   }
   else
   {
        cout << "Invalid statement.\n";
   }
    return 0;
}

God you scrubs I did this in like 5 seconds.

Stop doing integer division.
« Last Edit: January 13, 2012, 07:38:54 PM by Otis Da HousKat »