如何检查用户输入?

时间:2015-09-06 18:00:03

标签: c++

我希望能够使除'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;
}

4 个答案:

答案 0 :(得分:1)

通常使用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;
}

答案 1 :(得分:0)

首先,你可以做一些像if(输入==&#34; C&#34; ||输入==&#34; c&#34;) 或者您可以将输入转换为大写/小写

其次,你可以添加一个类似&#34的else语句;请输入一个有效的命令&#34;。玩弄它,你甚至可以使用循环来等待正确的输入!

答案 2 :(得分:0)

我的方法是针对所有有效输入的容器测试输入。

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

答案 3 :(得分: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;
    }
}