I am taking a C++ programming course and working on a project. I am having an issue with the bad path of this problem. It is a user based input problem for temperature conversion. I have the happy path working great but the bad path is not acting like I expect it to. To my understanding, if I hit the bad path, it should wait for me to re-enter another input before exiting. In my code though, it just prints all the info in the bad path and exits.
#include "unitConversionHeader.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <typeinfo>
#include <any>
using namespace std;
void tempOutputs(int, char);
void tempConversion() {
// Parent function.Collect the inputs, handle the conversions and checks then return the result.
int temperature;
char units;
char retry;
char killswitch;
bool running;
running = true;
cout << "This program converts Temperatures from Fahrenheit to Celsius and vice versa. \n";
while (running) {
cout << "Please enter your temperature: \n";
cin >> temperature;
cout << "\n";
if (cin.fail()) {
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
if (units == 'Y') {
break;
}
else {
cout << "Too many incorrect attempts. Exiting program now. \n";
cout << "Thank you. Goodbye. \n";
running = false;
}
}
else if (cin.good()) {
cout << "Please enter your units (F/C): ";
// convert to upper to eliminate risk of inputting the wrong value, lowercase is sufficient
cin >> units;
units = toupper(units);
cout << "\n";
if (units == 'F' || units == 'C') {
tempOutputs(temperature, units);
cout << "\n";
cout << " Do you want another conversion? (Y/N): \n";
cin >> killswitch;
killswitch = toupper(killswitch);
}
if (killswitch == 'N') {
cout << "Thank you. Goodbye. \n";
running = false;
}
}
}
}
void tempOutputs(int temperature, char units) {
// After receiving the inputs we add the conversion logic and then run the calculations
string lhs = "Celsius";
string rhs = "Celsius";
int calculation;
if (units == 'F') {
lhs = "Fahrenheit";
}
else {
rhs = "Fahrenheit";
}
// taken from header
calculation = unitConversion(temperature, units);
cout << "A temperature of " << temperature << " degrees " << lhs << " is equivalent to " << calculation << " degrees " << rhs << ". \n";
}
When I run the problem, if I enter a char for the first input and hit the cin.fail logic I just get the following output:
This program converts Temperatures from Fahrenheit to Celsius and vice versa.
Please enter your temperature:
f
The value you have entered is incorrect or not supported. Would you like to try again? (Y/N):
Too many incorrect attempts. Exiting program now.
Thank you. Goodbye.
What should happen is that it hangs and waits for me to enter another value to confirm how I would like to proceed.
Before these statements
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
you should write
#include <limits>
//...
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
to clear the input buffer. Otherwise the variable retry
reads the current content of the buffer that according to the provided by you output is the letter f
..
Also it seems using the break statement
cout << "The value you have entered is incorrect or not supported. Would you like to try again? (Y/N): \n";
cin >> retry;
units = toupper(retry);
if (units == 'Y') {
break;
contradicts the prompt.