Temperature converter

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;
}
    
asked by anonymous 26.02.2018 / 14:27

3 answers

3

Whenever you use a numeric literal make sure it is the literal of the right type. In type float the literal always has a decimal point and ends with a f or F . Your 5 by 9 division is using integers, so 5 divided by 9 gives 0 and then anything multiplied by 0 gives 0.

You do not even have to declare the type if you use the right literal. The compiler infers you.

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

Keeping your formula the right way and simplified:

#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;      
}
    
26.02.2018 / 14:59
1

To convert Fahrenheit to 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;
}

    
26.02.2018 / 14:49
1

You are confusing with the 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 Just Right on this Line celsius = (fah - 32) / (1.8);     

26.02.2018 / 14:52