【问题标题】:Is there a better way to format this while-loop?有没有更好的方法来格式化这个while循环?
【发布时间】:2018-03-30 18:57:29
【问题描述】:

在我正在创建的程序上搜索潜在错误时,我得到了 “[错误] ISO C++ 禁止指针和整数之间的比较 [-fpermissive]。”

错误来自我的 while 循环,我打算让程序只接受来自用户的以下输入。现在,我正在考虑为此使用一个数组。

我应该如何解决这个问题?如果有人有更多详细信息,我很乐意通过展示我的身份(最近)向他们提供。

int main () // While using one of the three functions to make conversions, 
// the program will create the table based on following information. 
{
    char dimension;
    double u1, u2;  
    int starting_value, ending_value, increment, startingUnitDigits, 
    endingUnitDigits;
    cout << "-------------------------------------------------------------------------------------------------";
    cout << "Please answer the following questions as they appear.";
    cout << "Check spelling, use correct abbreviation for units of measurement, and avoid negative values.";
    cout << "-------------------------------------------------------------------------------------------------";
    cout << "Enter the dimensions from the following choices: length, mass and time. (i.e.: 'length,' 'Length').";
    while (!(dimension == "length" || dimension == "Length" || dimension == "mass" || dimension == "Mass" || dimension == "time" || dimension == "Time"))
    {
        cout << "----------------------------------------------------------------------------------------------------";
        cout << "Error! Input for Dimension, " << dimension << ", was either an invalid choice or typed incorrectly.";
        cout << "Please know that this programs accepts length, mass, and time only!";  
        cout << "----------------------------------------------------------------------------------------------------";
        cout << "Enter the dimensions from the following choices: length, mass and time. (i.e.: 'length,' 'Length').";
        cin >> dimension;   

【问题讨论】:

  • char dimension 它的 1 个字符,您正在检查维度 == "length" .. 等
  • @Catholic_Ryanite 使用 std::string 维度;而不是 char 维度;并在循环之前初始化它
  • 顺便说一句,您的标题具有误导性,并且与问题没有真正的关系,格式并不是真正的问题(除了可能的意图不佳)
  • 使用while (true) { cin &gt;&gt; dimension; if (...) { print error message } else {break;}}
  • @tobi303 如果他改成std::string,则默认为空字符串。

标签: c++ arrays string file while-loop


【解决方案1】:

cmets已经很详尽了,我就总结一下吧:

dimension 是一个 char,您无法将其与字符串文字进行比较,因此会出现错误。您应该改用std::string

一旦你改变了问题,你在从cin读取任何内容之前检查用户输入。你应该反过来做。

如果您使用容器并尝试在该容器中查找用户输入,您的条件将更具可读性。

所以应该是这样的:

std::vector<std::string> correct_input{"Foo","foo","f00"};
while(true) {
    std::string dimension;
    std::cin >> dimension;
    auto it = std::find(correct_input.begin(),correct_input.end(),dimension);
    if (std::cin.fail() || it==correct_input.end()) {
        // print error and continue to ask for input
        std::cin.clear();  // clear possible error flags
    } else {
       // do something with input
       break;
    }
}

【讨论】:

  • 抱歉,这不是我要找的。​​span>
  • 这不是您要找的吗?来吧,这就是你的评论?你在找什么??
  • 我的意图是让用户询问他们是否想要尺寸的长度、质量或时间。如果用户出于某种原因没有正确输入他们的输入,程序应该让他们知道他们必须使用正确的拼写。
  • @Catholic_Ryanite 这就是我的代码所做的,您只需填写空白
  • 我认为这将很快降级为一些讨论性教程式的东西,在某种程度上,这里不赞成。 @Catholic_Ryanite,如果您正在寻找某种交互式向导/演练/菜单/guide-me-by-hand-error-handling/等,请在问题中包含具体信息。而且,坦率地说,最好将其作为一个新问题,因为您现在在这里打开的问题是关于转换警告/错误,而您现在提出的这个请求是完全不同的故事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-15
  • 2011-03-02
  • 1970-01-01
  • 2022-01-10
相关资源
最近更新 更多