Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

when I runt this code in C++, I seem to be getting errors and I need help fixing

ID: 3890627 • Letter: W

Question

when I runt this code in C++, I seem to be getting errors and I need help fixing it.

#include <iostream>
using namespace std;

int main()
{

double temperature = 0.0;
char u;

cout << "Enter a temperature:" << endl;
cin >> temperature;

cout << "What unit is the temperature in? (c or f)" << endl;
cin >> u;

if(u=='c')
{
double f = ('C' * 9.0 / 5.0) + 32.0;
cout << temperature << "C = " << f << "F" << endl;

}else if( u=='f')
{
double c = ('F' - 32.0) * 5.0 / 9.0;
cout << temperature << "F = " << c << "C" << endl;

}else
{
cout << "ERROR" << endl;
};

}

The error message is...


Executing error-2 test case:
Executing stdout stream test
Executing stderr stream test
Executing error-1 test case:
Executing stdout stream test
Executing stderr stream test
Executing c-to-f-1 test case:
Executing stdout stream test
Failure:
Expected "100C = 212F"
Received "100C = 152.6F"
Executing stderr stream test
Executing f-to-c-3 test case:
Executing stdout stream test
Failure:
Expected "47.2F = 8.44444C"
Received "47.2F = 21.1111C"
Executing stderr stream test
Executing f-to-c-2 test case:
Executing stdout stream test
Failure:
Expected "32F = 0C"
Received "32F = 21.1111C"
Executing stderr stream test
Executing c-to-f-3 test case:
Executing stdout stream test
Failure:
Expected "19.12C = 66.416F"
Received "19.12C = 152.6F"
Executing stderr stream test
Executing c-to-f-2 test case:
Executing stdout stream test
Failure:
Expected "34C = 93.2F"
Received "34C = 152.6F"
Executing stderr stream test
Executing f-to-c-1 test case:
Executing stdout stream test
Failure:
Expected "212F = 100C"
Received "212F = 21.1111C"
Executing stderr stream test
=======================================================================
6 errors found!

Explanation / Answer

Hi,

I have fixed the issue and highlighted the code changes below

#include <iostream>
using namespace std;
int main()
{
double temperature = 0.0;
char u;
cout << "Enter a temperature:" << endl;
cin >> temperature;
cout << "What unit is the temperature in? (c or f)" << endl;
cin >> u;
if(u=='c')
{
double f = (temperature * 9.0 / 5.0) + 32.0;
cout << temperature << "C = " << f << "F" << endl;
}else if( u=='f')
{
double c = (temperature - 32.0) * 5.0 / 9.0;
cout << temperature << "F = " << c << "C" << endl;
}else
{
cout << "ERROR" << endl;
}

}

Output: