Below is the code that will not increment correctly. \"END\" number will not sho
ID: 3651332 • Letter: B
Question
Below is the code that will not increment correctly. "END" number will not show the program stops at the number before it.
#include <iostream>
#include <string>
#include <stdlib.h>
#include<iomanip>
using namespace std;
void displayMenu();
// functions
char getMenuSelection();
void getStartEndAndIncrement(int&, int&, int&);
double CtoF(double);
double FtoC(double);
void displayTable(char, int, int, int);
void displayMenu()
// display the main menu
{
cout << "Welcome to the temperatures convertor program" << endl;
cout << "Please select one of the following options: " << endl;
cout << "(F/f) From Fahrenheit to Celsius" << endl;
cout << "(C/c) From Celsius to Fahrenheit" << endl;
cout << "(Q/q) Exit" << endl;
}
char getMenuSelection()
// menu selection from the user
{
char selection = '';
cout << "Please select an option: ";
cin >> selection;
while (tolower(selection) != 'f' && tolower(selection) != 'c' && tolower(selection) != 'q')
{
cout << "-- Invalid selection: try again --" << endl;
cout << endl;
displayMenu();
cout << "Please select an option: ";
cin >> selection;
cin.clear();
}
return selection;
}
void getStartEndAndIncrement(int& start, int& end, int& increment)
// Asks for the start, end and increment values
{
cout << "Enter 3 integers: starting temperature, ending temperature, and an increment" << endl;
string tmpStart = "", tmpEnd = "", tmpIncre = "";
cout << "starting temperature: ";
cin >> tmpStart; cin.clear();
cout << "ending temperature: ";
cin >> tmpEnd;
cin.clear();
cout << "increment: ";
cin >> tmpIncre;
cin.clear();
start = atoi(tmpStart.c_str());
end = atoi(tmpEnd.c_str());
increment = atoi(tmpIncre.c_str());
}
double CtoF(double celsius)
// converts a celsius temperature to Fahrenheit
{
return (9.0/5.0)*celsius + 32; } double FtoC(double fahrenheit)
// Converts a Fahrenheit temperature to celsius
{
return (5.0/9.0)*(fahrenheit-32);
}
void displayTable(char option, int startTemp, int endTemp, int increment)
// Displays table
{
cout << "------------------------------" << endl;
cout << setiosflags(ios::right);
cout << setiosflags(ios::fixed);
if (option == 'c')
{
cout << setw(6) << "C" << (char)248 << setw(8) << "F" << (char)248 << endl;
while (endTemp > startTemp)
{
cout << setw(8) << startTemp << setw(8) << setprecision(1) << CtoF(startTemp) << endl;
startTemp += increment;
}
if (startTemp != endTemp) cout << setw(8) << endTemp << setw(8) << setprecision(1) << CtoF(endTemp) << endl;
}
else
{
cout << setw(6) << "F" << (char)248 << setw(8) << "C" << (char)248 << endl;
while (endTemp > startTemp) { cout << setw(8) << startTemp << setw(8) << setprecision(1) << FtoC(startTemp) << endl; startTemp += increment;
}
if (startTemp != endTemp) cout << setw(8) << endTemp << setw(8) << setprecision(1) << FtoC(endTemp) << endl;
}
}
int main() { char usrSelection = '';
int startTemp = 0;
int endTemp = 0;
int increment = 0;
displayMenu();
usrSelection = getMenuSelection();
while (usrSelection != 'q')
{
getStartEndAndIncrement(startTemp, endTemp, increment);
displayTable(usrSelection, startTemp, endTemp, increment);
cout << endl;
displayMenu();
usrSelection = getMenuSelection();
return 0; }
Explanation / Answer
please rate - thanks
I corrected 2 other items, all are highlighted
#include <iostream>
#include <string>
#include <stdlib.h>
#include<iomanip>
using namespace std;
void displayMenu();
// functions
char getMenuSelection();
void getStartEndAndIncrement(int&, int&, int&);
double CtoF(double);
double FtoC(double);
void displayTable(char, int, int, int);
void displayMenu()
// display the main menu
{
cout << "Welcome to the temperatures convertor program" << endl;
cout << "Please select one of the following options: " << endl;
cout << "(F/f) From Fahrenheit to Celsius" << endl;
cout << "(C/c) From Celsius to Fahrenheit" << endl;
cout << "(Q/q) Exit" << endl;
}
char getMenuSelection()
// menu selection from the user
{
char selection = '';
cout << "Please select an option: ";
cin >> selection;
while (tolower(selection) != 'f' && tolower(selection) != 'c' && tolower(selection) != 'q')
{
cout << "-- Invalid selection: try again --" << endl;
cout << endl;
displayMenu();
cout << "Please select an option: ";
cin >> selection;
cin.clear();
}
return tolower(selection);
}
void getStartEndAndIncrement(int& start, int& end, int& increment)
// Asks for the start, end and increment values
{
cout << "Enter 3 integers: starting temperature, ending temperature, and an increment" << endl;
string tmpStart = "", tmpEnd = "", tmpIncre = "";
cout << "starting temperature: ";
cin >> tmpStart; cin.clear();
cout << "ending temperature: ";
cin >> tmpEnd;
cin.clear();
cout << "increment: ";
cin >> tmpIncre;
cin.clear();
start = atoi(tmpStart.c_str());
end = atoi(tmpEnd.c_str());
increment = atoi(tmpIncre.c_str());
}
double CtoF(double celsius)
// converts a celsius temperature to Fahrenheit
{
return (9.0/5.0)*celsius + 32; } double FtoC(double fahrenheit)
// Converts a Fahrenheit temperature to celsius
{
return (5.0/9.0)*(fahrenheit-32);
}
void displayTable(char option, int startTemp, int endTemp, int increment)
// Displays table
{
cout << "------------------------------" << endl;
cout << setiosflags(ios::right);
cout << setiosflags(ios::fixed);
if (option == 'c')
{
cout << setw(6) << "C" << (char)248 << setw(8) << "F" << (char)248 << endl;
while (endTemp >= startTemp)
{
cout << setw(8) << startTemp << setw(8) << setprecision(1) << CtoF(startTemp) << endl;
startTemp += increment;
}
if (startTemp-increment != endTemp) cout << setw(8) << endTemp << setw(8) << setprecision(1) << CtoF(endTemp) << endl;
}
else
{
cout << setw(6) << "F" << (char)248 << setw(8) << "C" << (char)248 << endl;
while (endTemp >= startTemp) { cout << setw(8) << startTemp << setw(8) << setprecision(1) << FtoC(startTemp) << endl; startTemp += increment;
}
if (startTemp-increment!= endTemp) cout << setw(8) << endTemp << setw(8) << setprecision(1) << FtoC(endTemp) << endl;
}
}
int main() { char usrSelection = '';
int startTemp = 0;
int endTemp = 0;
int increment = 0;
displayMenu();
usrSelection = getMenuSelection();
while (usrSelection != 'q')
{
getStartEndAndIncrement(startTemp, endTemp, increment);
displayTable(usrSelection, startTemp, endTemp, increment);
cout << endl;
displayMenu();
usrSelection = getMenuSelection();
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.