Temperature converter

Asked

Viewed 737 times

1

When I type the temperature in Fahrenheint the program returns 0 as a result, where is the error?

#include <cstdlib>
  #include <iostream>
  #include <conio.h>
  #include <string.h>
  #include <math.h>

  using namespace std;

  int main(int argc, char *argv[])
  {
    float celsius = 0, fah = 0;
    char tecla;

    cout << "Digite a temperatura em C°:"; cin >> celsius;

    fah = (9 * celsius + 160) / 5;

    cout << "Temperatura em F°:" << fah << endl;       

    cout << "Digite a temperatura em F°:"; cin >> fah;

    celsius = (fah - 32) * (5/9);

    cout << "Temperatura em C°:" << celsius << endl;      

    system("PAUSE");
    return EXIT_SUCCESS;
}
  • Review the formula you are using for conversion from Fahrenheint to Celsius

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

3 answers

3

Whenever using a numeric literal make sure to be the literal of the right type. In type float the literal always has a decimal point and ends with a f or F. Its division of 5 by 9 is using integers, so 5 divided by 9 is 0 and then everything that multiplies by 0 is 0.

You don’t even have to declare the type if you use the right literal. The compiler infers for you.

Be careful to use 5.0 only or do the direct account if not using the suffix f the guy will be double and in some chaos may give a slightly different result. It may not affect this case, but learn how to do right always.

keeping your formula the right and simplified way:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    auto celsius = 0.0f;
    cout << "Digite a temperatura em C°:"; cin >> celsius;
    auto fah = (9 * celsius + 160) / 5;
    cout << "Temperatura em F°:" << fah << endl;       
    cout << "Digite a temperatura em F°:"; cin >> fah;
    cout << "Temperatura em C°:" << (fah - 32) * (5.0f / 9) << endl;      
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

1

To convert Fahrenheit in Celsius, subtract 32 and divide by 1,8.

°C = (°F 32) 1, 8

See Example:

#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <math.h>

using namespace std;
int main(int argc, char *argv[]){

    float celsius = 0, fah = 0;
    char tecla;

    cout << "Digite a temperatura em Celsius:"; cin >> celsius;

    fah = (9 * celsius + 160) / 5;

    cout << "Temperatura em Fahrenheit:" << fah << endl;       


    cout << "Digite a temperatura em Fahrenheit:"; cin >> fah;

    celsius = (fah - 32) / 1.8;

    cout << "Temperatura em Celsius:" << celsius << endl;      

    system("PAUSE");
    return EXIT_SUCCESS;
}

inserir a descrição da imagem aqui

1

You are Confusing with Formula and the type of variable used, here is the code

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

using namespace std;

int main(int argc, char *argv[]){

float celsius = 0, fah = 0;
char tecla;

  cout << "Digite a temperatura em C°:"; cin >> celsius;

  fah = (9 * celsius + 160) / 5;

  cout << "Temperatura em F°:" << fah << endl;       

  cout << "Digite a temperatura em F°:"; cin >> fah;

  celsius = (fah - 32) / (1.8);

  cout << "Temperatura em C°: " << celsius << endl;      

  system("PAUSE");
  return EXIT_SUCCESS;
  }

You Were Wrong Just On That Line
Celsius = (Fah - 32) / (1.8);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.