C++ Program Converts Fahrenheit to Celsius

C program to convert Fahrenheit to Celsius always prints zero

5/9 will result in integer division, which will = 0

Try 5.0/9.0 instead.

Converting from Fahrenheit to Celsius and viceversa in C

change (5/9) to 5.0/9.0. If you use integer division, this gives you 0 after rounding.

You should do the same for 9/5

Program to Convert Fahrenheit to Celsius

It seems to be a compiler issue. My compiler (Emulated Turbo C++ 3.0) was not able to save my edits properly. So I went to C:\TC\Bin\ filename.c and opened the file in Notepad. Corrected the errors and compiled it again.

Now it works :)

Fahrenheit to Celsius in C

Two issues here:

First, you attempt to assign string constants to single characters:

char fArg = "-f";
char cArg = "-c";

Your compiler should have warned you about this. Change them to character arrays:

char fArg[] = "-f";
char cArg[] = "-c";

And modify the calls to strcmp to remove the address-of operator & from these variables.

Second, the first command line argument is actually the name of the program, not the first user argument. So anyplace you access an element of argv, increase the index by 1.

sscanf(argv[2], "%f", &arg2);
if(strcmp(argv[1], cArg) == 0)
...
else if(strcmp(argv[1], fArg) == 0)

C++ program converts fahrenheit to celsius

(5/9) will by default be computed as an integer division and will be zero. Try (5.0/9)

C Temperature Conversion Program Keeps Outputting 0 For Fahrenheit to Celsius


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

Here, 5/9 is integer division, its result is 0, change it to 5.0/9


And in several lines, you are using

scanf("%f", &*Celsius);

&* is not necessary, simply scanf("%f", Celsius); would do.

Fahrenheit to Celsius Converter C++

fixed the issue for you!

#include <iostream> //cout
#include <conio.h> //getch

using namespace std;

int main()
{
double celsius, fahrenheit, temp;

char unit;

cout << "Enter the temperature you wish to convert followed by F for Fahrenheit or C for Celsius: " << endl;
cin >> temp;
cin >> unit;
switch (unit){
case 'F':
celsius = (5.0 / 9.0) * (temp - 32.0);
cout << celsius << " degrees celsius";
break;

case 'f':
celsius = (5.0 / 9.0) * (temp - 32.0);
cout << celsius << " degrees celsius";
break;

case 'C':
fahrenheit = (9.0 / 5.0) * temp + 32.0;
cout << fahrenheit << " degrees fahrenheit";
break;

case 'c':
fahrenheit = (9.0 / 5.0) * temp + 32.0;
cout << fahrenheit << " degrees fahrenheit";
break;

default:
cout << "Invalid Format";
break;
}

_getch();

return 0;
}

EDIT: Switch case statements have a "fall through" behaviour. Thus, you can put the cases for 'F' and 'f' in the together (and similarly 'C' and 'c') as suggested by a comment on this post, by doing the following:

#include <iostream> //cout
#include <conio.h> //getch

using namespace std;

int main()
{
double celsius, fahrenheit, temp;

char unit;

cout << "Enter the temperature you wish to convert followed by F for Fahrenheit or C for Celsius: " << endl;
cin >> temp;
cin >> unit;
switch (unit){
case 'F':
case 'f':
celsius = (5.0 / 9.0) * (temp - 32.0);
cout << celsius << " degrees celsius";
break;

case 'C':
case 'c':
fahrenheit = (9.0 / 5.0) * temp + 32.0;
cout << fahrenheit << " degrees fahrenheit";
break;

default:
cout << "Invalid Format";
break;
}

_getch();

return 0;
}

If this is a school assignment, this is probably what they want you to do (and why you were supposed to use the switch statement.)



Related Topics



Leave a reply



Submit