Reputation: 2265
Relevant Info: This is a Visual Studio C++ Console Application
I was doing some basic C++ to keep somewhat fresh when I experienced an issue. This is the entirety of my cpp
file:
#include <iostream>
#include <string>
double CelsiusToFahrenheit(double);
double FahrenheitToCelsius(double);
void GetUserInput();
int main() {
GetUserInput();
}
double CelsiusToFahrenheit(double celsiusNum) {
return ((celsiusNum * (9.0 / 5.0)) + 32.0);
}
double FahrenheitToCelsius(double fahrenheitNum) {
return ((fahrenheitNum - 32.0) * (5.0/9.0));
}
void GetUserInput() {
char uiChoice;
double uiNumber;
std::cout << "Please Select an Option: " << std::endl;
std::cout << "Enter C for Celsius to Fahrenheit conversion, Enter F for Fahrenheit to Celsius conversion: ";
std::cin >> uiChoice;
if (uiChoice == 'C') {
std::cout << "Please enter the degrees in Celsius: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees C in Fahrenheit is " << CelsiusToFahrenheit(uiNumber) << " degrees F." << std::endl;
}
else if (uiChoice == 'F') {
std::cout << "Please enter the degrees in Fahrenheit: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees F in Celsius is " << FahrenheitToCelsius(uiNumber) << " degrees C." << std::endl;
}
else {
std::cout << "Invalid choice, please try again." << std::endl;
GetUserInput();
}
return;
}
Inside of GetUserInput()
, when running the line std::cin >> uiChoice;
, if a user enters more than 1 character for the input, I get multiple executions of the else
statement.
Example output:
I was wondering, how can I either
a) Prevent multiple inputs
b) Only fire the else once when multiple inputs are entered
ALSO
I have tried initializing uiChoice
as a string and using compare()
, but the results have been... weird? An example of that function:
void GetUserInput() {
std::string uiChoice;
double uiNumber;
std::cout << "Please Select an Option: " << std::endl;
std::cout << "Enter C for Celsius to Fahrenheit conversion, Enter F for Fahrenheit to Celsius conversion: ";
std::cin >> uiChoice;
if (uiChoice.compare("C")) {
std::cout << "Please enter the degrees in Celsius: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees C in Fahrenheit is " << CelsiusToFahrenheit(uiNumber) << " degrees F." << std::endl;
}
else if (uiChoice.compare("F")) {
std::cout << "Please enter the degrees in Fahrenheit: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees F in Celsius is " << FahrenheitToCelsius(uiNumber) << " degrees C." << std::endl;
}
else {
std::cout << "Invalid choice, please try again." << std::endl;
GetUserInput();
}
return;
}
In the following output, you can see that any character entered will satisfy the criteria for if (uiChoice.compare("C"))
.
output:
I don't have much experience using the compare()
function, so this very well could be me interpreting the uses of compare()
wrong.
EDIT:
Updated GetUserInput()
with std::getline()
void GetUserInput() {
std::string uiChoice;
double uiNumber;
std::cout << "Please Select an Option: " << std::endl;
std::cout << "Enter C for Celsius to Fahrenheit conversion, Enter F for Fahrenheit to Celsius conversion: ";
std::getline(std::cin, uiChoice);
if (uiChoice.compare("C")) {
std::cout << "Please enter the degrees in Celsius: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees C in Fahrenheit is " << CelsiusToFahrenheit(uiNumber) << " degrees F." << std::endl;
}
else if (uiChoice.compare("F")) {
std::cout << "Please enter the degrees in Fahrenheit: ";
std::cin >> uiNumber;
std::cout << std::endl << uiNumber << " degrees F in Celsius is " << FahrenheitToCelsius(uiNumber) << " degrees C." << std::endl;
}
else {
std::cout << "Invalid choice, please try again." << std::endl;
GetUserInput();
}
return;
}
Output:
Upvotes: 0
Views: 459
Reputation: 87959
You are misunderstanding the compare
method. The simple way to do what you want is to use ==
if (uiChoice == "C") {
And has already been said getline
is the answer to your other problem. But please read this very important issue when mixing getline
with reading numbers using >>
Why does std::getline() skip input after a formatted extraction?
This issue catches everybody out at first.
Upvotes: 1