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

-50 f should = -45.6 C Also outputs same numbers for CtoF and FtoC #include <ios

ID: 3638148 • Letter: #

Question

-50 f should = -45.6 C
Also outputs same numbers for CtoF and FtoC


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

using namespace std;

//function prototypes
void displayMenu();
char getMenuSelection();
void getStartEndAndIncrement(int&, int&, int&);
double CtoF(double);
double FtoC(double);
void displayTable(char, int, int, int);

//display main menu
void displayMenu()
{
cout << "Welcome to the temperature convertor program" << endl;
cout << "Please select one of the following options: " << endl;
cout << "Press (F/f) to convert from Fahrenheit to Celsius" << endl;
cout << "Press (C/c) to convert from Celsius to Fahrenheit" << endl;
cout << "Press (Q/q) to Quit" << endl;
}

//get the menu selection from the user and return selection
char getMenuSelection()
{
char selection = '';
cout << "Please select an option: ";
cin >> selection;

while (tolower(selection) != 'f' && tolower(selection) != 'c' && tolower(selection) != 'q')
{
cout << "Error: Invalid selection please try again." << endl;
cout << endl;
//re-prompt user with menu
displayMenu();
cout << "Please select an option: ";
cin >> selection;

cin.clear();
}
return selection;
}

//get start, end and increment values
void getStartEndAndIncrement(int& start, int& end, int& increment)
{
cout << "Enter 3 integers: starting temperature, ending temperature, and an increment" << endl;
string tempStart = "", tempEnd = "", tempIncrement = "";
cout << "Starting temperature: ";
cin >> tempStart;
cout << "Ending temperature: ";
cin >> tempEnd;
cout << "Increment: ";
cin >> tempIncrement;

start = atoi(tempStart.c_str());
end = atoi(tempEnd.c_str());
increment = atoi(tempIncrement.c_str());
}

//converts C to F
double CtoF(double celsius)
{
return double (9.0 / 5.0) * celsius + 32;
}

//converts F to C
double FtoC(double fahrenheit)
{
return double (5.0 / 9.0) * fahrenheit - 32;
}

//displays table
void displayTable(char option, int startTemp, int endTemp, int increment)
{
cout << endl;
cout << "**********************" << endl;
cout << endl;
cout << setiosflags(ios::right);
cout << setiosflags(ios::fixed);

//display C to F
if (option == 'c')
{
cout << setw(6) << (char)248 << "C" << setw(9) << (char)248 << "F" << endl;

while (endTemp >= startTemp)
{
cout << setw(8) << startTemp << setw(9) << setprecision(1)
<< CtoF(startTemp) << endl;
startTemp += increment;
}
//output endTemp
if (startTemp <= endTemp)
{
cout << setw(8) << endTemp << setw(9) << setprecision(1)
<< CtoF(endTemp) << endl;
}
}
else //display F to C
{
cout << setw(6) << (char)248 << "F" << setw(9) << (char)248 << "C" << endl;

while (endTemp >= startTemp)
{
cout << setw(8) << startTemp << setw(9) << setprecision(1)
<< CtoF(startTemp) << endl;
startTemp += increment;
}
//output endTemp
if (startTemp <= endTemp)
{
cout << setw(8) << endTemp << setw(9) << setprecision(1)
<< CtoF(endTemp) << endl;
}
}
}

int main()
{
char userSelection = '';
int startTemp = 0;
int endTemp = 0;
int increment = 0;

displayMenu();
userSelection = getMenuSelection();

//sentinel value
while (userSelection != 'q')
{
getStartEndAndIncrement(startTemp, endTemp, increment);
displayTable(userSelection, startTemp, endTemp, increment);
cout << endl;
displayMenu();
userSelection = getMenuSelection();
}
return 0;
}

Explanation / Answer

Your error is in the FtoC function: //converts F to C double FtoC(double fahrenheit) { return double (5.0 / 9.0) * fahrenheit - 32; } The order of operation is incorrect. Take this example, 212F = 100C (these are boiling points of water in F and in C. --Using your formula above: (5.0/9.0) * 212 - 32 = 85.778C This is of course incorrect. You must subtract 32 from 212 before multiplying --Updated formula with parenthesis for correct order of operations: (5.0/9.0) * (212 - 32) = 100C So the FtoC function should be updated as follows: //converts F to C double FtoC(double fahrenheit) { return double (5.0 / 9.0) * ( fahrenheit - 32 ); } Good luck!!