如何检查用户输入

How to check user input?

本文关键字:用户 输入 检查 何检查      更新时间:2023-10-16

所以我希望能够使所有用户输入无效,除了某个单词,如'K''C'。我一点也不确定该怎么做。因此,如果他们把它拼错为"celcius"或"husdhfjae",我的程序就会显示为"Input invalid, please enter K or C."

请不要太复杂,因为我才刚刚开始。谢谢你:)
//  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;
}

通常使用whiledo...while循环检查用户输入。这个想法很简单,您总是返回到相同的错误消息,并再次读取输入,直到它是正确的。

在单个string中放置有效选项的优点是允许轻松地添加或删除选项,而无需处理长if条件。

我相信像这样简单的事情就可以做到:

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;
}

首先,您可以执行如下操作if(input == "C" || input == "C")或者您可以将输入转换为小写/大写

第二,您可以添加一个else语句,表示"请输入一个有效的命令"。你甚至可以使用循环来等待正确的输入!

我的方法是根据所有有效输入的容器测试输入。

#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;
}

需要一个while循环。这可能是最简单的方法了。

#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;
    }
}

我有同样的问题,而得到正确的用户输入,我写了一个简单的解决方案,我希望它将有助于每个人开始使用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;
}