Reputation: 29
SO I want to be able to invalidate all user input except a certain word, like 'K'
or 'C'
. I'm not sure at all how to do this. So if they mispell it to "celcius" or "husdhfjae", my program would say "Input invalid, please enter K or C."
Please nothing too complicated, because I just started. Thank you :)
// CS 575,HW #1B, Ravela Smyth
// This program converts from Fahrenheit to Celsius or Kelvin
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
double Fahrenheit, celsius, kelvin;
cout << "Hi! What is the weather today in Fahrenheit?? " << endl;
cin >> Fahrenheit;
cout << "Would you like to convert this temperature to Celsius or Kelvin? (C/K)" << endl;
cin >> input;
if (input == "C")
{
celsius = (5 * (Fahrenheit - 32)) / 9;
cout << "Today's weather in Celsius is " << celsius << " degrees! " << endl;
}
else if (input == "c")
{
celsius = (5 * (Fahrenheit - 32)) / 9;
cout << "Today's weather in Celsius is " << celsius << " degrees! " << endl;
}
else if (input == "K")
{
kelvin = (5 * (Fahrenheit + 459.67)) / 9;
cout << "Today's weather in Kelvin is " << kelvin << " degrees!" << endl;
}
else if (input == "k")
{
kelvin = (5 * (Fahrenheit + 459.67)) / 9;
cout << "Today's weather in Kelvin is " << kelvin << " degrees!" << endl;
}
return 0;
}
Upvotes: 0
Views: 8951
Reputation: 1
I had the same problem while getting the correct user input for that i wrote a simple solution i hope it will be helpfull for everyone getting started with c++.
//get user input
char input;
cin >> input;
//convert the input to lowercase
char Temp = tolower(input);
//check the input (not valid input will clear the user input)
while(!(cin >> input) || ((Temp != 'c') &&( Temp != 'k')){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please, try again: ";
}
//evalute input cases
switch (Temp)
{
case 'c':
/* celcius */
break;
case 'k':
/* Kelvin */
break;
}
Upvotes: 0
Reputation: 6875
Usually user inputs are checked using while
or do...while
loops.
The idea is simple, you always get back to the same error message and read again the input until it is correct.
The advantage of placing the valid options in the single string
is to allow easy addition or removal of the options without dealing with long if
conditions.
I believe something simple like this will do the job:
std::string valid_options("kKcC");
std::string input;
bool illegal_input;
std::cout << "Would you like to convert this temperature to Celsius or Kelvin? (C/K)" << std::endl;
std::cin >> input;
// check that only one letter was provided and it belongs to the valid options
while (input.size() != 1 || valid_options.find(input) == std::string::npos)
{
std::cout << "Input invalid, please enter K or C.\n";
std::cin >> input;
}
Upvotes: 1
Reputation: 6358
You need a while loop. This is probably the simplest way to do it.
#include <iostream>
#include <string>
int main()
{
std::string word;
std::cin >> word;
//Keep asking for a word until this condition is false, i.e.
//word will be equal to one of these letters
while(word != "C" && word != "c" && word != "K" && word != "k")
{
std::cout << "Invalid temperature type: " << word << " Use 'K' or 'C'" << std::endl;
std::cin >> word;
}
if (word == "C" || word == "c")
{
std::cout << "Celsius" << std::endl;
}
else if (word == "K" || word == "k")
{
std::cout << "Kelvin" << std::endl;
}
}
Upvotes: 0
Reputation: 1255
My approach is to test the input against a container of all valid inputs.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
bool valid(std::string s,std::vector<std::string> y)
{
std::sort(y.begin(), y.end());
return std::binary_search(y.begin(), y.end(), s);
}
int main()
{
std::string s;
do
{
std::cout << "Enter K or C: ";
std::cin >> s;
} while (!valid(s, { "K","C","k","c" }));
std::cout << "good!" << std::endl;
return 0;
}
Upvotes: 0
Reputation: 463
First, you can do something like if(input == "C" || input == "c") Or you can convert the input to lower/upper case
Second, you can add an else statement that says something like "please enter a valid command". Play around with it, you can even use loops to wait for correct input!
Upvotes: 0