本文介绍了你可以解释如何布尔可以控制回路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++(目前)初学者程序员,我有一个概念上的问题。

I'm a beginner programmer in C++ (currently), and I've got a conceptual question.

我试图过滤一霉素输入,以确保它是01-04之间的一或二位的整数,并且如果它不是,以产生一个错误,并要求一个新的输入。

I'm trying to filter a cin input to ensure that it is a one-or-two-digit integer between 01-04, and if it isn't, to produce an error and ask for a new input.

我还使用地图来给用户一个选项列表,在有效的选择,线路输入(整数),通过任何几种方法制作一个相关的结果,但我会问其他地方这个问题的一个更具体的版本。

I'm also using map to give the user a list of options that, upon valid selection, routes inputs (integers) through any of several methods to produce a relevant result, but I'll ask a more specific version of this question elsewhere.

我发现code的片段在,是为了验证输入。我有点明白了,除非布尔条件设置while循环中。因为我不明白,这使得它很难编辑或确保我操纵它的权利。

I found a snippet of code at http://www.cplusplus.com/forum/beginner/26821/ that is meant to validate an input. I sort of get it, except where the boolean condition is set inside the while loop. Because I don't understand it, it makes it very difficult to edit or make sure that I'm manipulating it right.

下面是例子code:

int main()
{
    int num;
    bool valid = false;

    while (!valid)
    {
        valid = true; //Assume the cin will be an integer.

        cout << "Enter an integer value: " << endl;
        cin >> num;

        if(cin.fail()) //cin.fail() checks to see if the value in the cin
                    //stream is the correct type, if not it returns true,
                    //false otherwise.
        {
            cin.clear(); //This corrects the stream.
            cin.ignore(); //This skips the left over stream data.
            cout << "Please enter an Integer only." << endl;
            valid = false; //The cin was not an integer so try again.
        }
    }

    cout << "You entered: " << num << endl;

    system("PAUSE");
    return 0;

这是我的code(整个事情,给上下文)。我不认为这是完整的,我只是想确保我使用布尔权利。

And here is my code (the entire thing, to give context). I don't think it's complete, I just want to make sure I'm using the boolean right.

float _tmain(float argc, _TCHAR* argv[])
{
    bool validInput = !true;

    map<string,int> Operations;
    Operations.insert(pair<string, int>("Addition", 01));
    Operations.insert(pair<string, int>("Subtraction", 02));
    Operations.insert(pair<string, int>("Multiplication", 03));
    Operations.insert(pair<string, int>("Division", 04));

    cout << "Welcome to OneOpCalc, what operation would you like to perform?" << endl;

    for(map<string, int>::iterator ii=Operations.begin(); ii!=Operations.end(); ++ii)
    {
        cout << (*ii).second << ": " << (*ii).first << endl;
    }

    while (!validInput)
    {
        cin >> operatorSelection;

        if (cin.fail() || operatorSelection < 4 || operatorSelection > 1)
        {
            cout << "Error: Invalid selection. Please choose a valid number." << endl << endl;
            cin.clear();
            cin.ignore();
        }
    }
}

确实而(!有效)的意思是当有效的?在我的头,它说虽然有效!有效,这很明显,将永远是假的。

Does while (!valid) mean "While valid is false"? In my head, it's saying "While valid is !valid", which obviously, would always be false.

编辑:感谢球员,我期待通过他们所有的答案。一个答案我一直得到那张过于笼统;我明白那个 !是不是,我理解翻转使用它的布尔的概念。然而,隐含的逻辑含义是什么让我困惑。在任何给定的声明,我习惯了的思维有效作为翻转有效价值的一种方式!;未测试的条件。它是用它来测试一个条件,即招数我的语法。换句话说,写而(!有效)字面上读我为而(NOTvalid),而不是而(有效== FALSE)。我不能让自己明白,为什么在这种情况下,!有效读为条件,而不是只是有点翻转。

Thanks for the answers guys, I'm looking through them all. One answer I keep getting goes too general; I understand that ! is NOT, and I understand the concept of flipping the bool using it. However the implicit logical implications are what confuse me. In any given statement, I am used to thinking of !valid as a way of flipping the valid value; Not testing a condition. It's the syntax of using it to test a condition that tricks me. In other words, writing while(!valid) reads literally to me as while(NOTvalid), not while(valid==false). I can't get myself to understand why in this case, !valid reads as a condition and not just a bit-flip.

推荐答案

循环(和IFS)按类型的前pression controled 布尔
而(!有效),前pression是!有效,操作员
不会应用到变量有效的价值。
而(!有效)办法(字面),而前pression
!有效(意思是无效)是真实的。

Loops (and ifs) are controled by an expression of type bool.In while ( !valid ), the expression is !valid, the operatornot applied to the value of the variable valid.while ( !valid ) means (literally) while the expression!valid (which means "not valid") is true.

对于剩下的,你复制code是pretty坏。我不会
使用它作为一个例子,如果我是你。

For the rest, the code you're copying is pretty bad. I wouldn'tuse it as an example if I were you.

对于自己的code:


  • _tmain 非常特别的微软。你不想来
    用它。如果你写一个控制台应用程序,只需使用
    (同样的事情适用于 _TCHAR ,而不是字符

  • _tmain is very particular to Microsoft. You don't want touse it. If your writing a console application, just use main.(Same thing holds for _TCHAR, rather than char.)

无论是 _tmain 也不可以返回浮动。回报
主要类型应始终 INT 。我不熟悉
_tmain ,但它要么 INT 无效。 (大概 INT ,如果
你在一个控制台程序。)

Neither _tmain nor main can return a float. The returntype of main should always be int. I'm less familiar with_tmain, but it's either int or void. (Probably int, ifyou're in a console mode program.)

!真。总是。 (编程比不同
真实的世界。有没有maybes。)为什么会更
比需要复杂?

!true is false. Always. (Programming is different thanthe real world. There are no maybes.) Why be morecomplicated than necessary?

有没有必要为标志变量都没有。您
可以这样写:

There's no need for the flag variable at all. Youcan just write:

CIN >> operatorSelection;
而(CIN || operatorSelection> 4 || operatorSelection&LT;!1){
    // ...
}

cin >> operatorSelection;while ( !cin || operatorSelection > 4 || operatorSelection < 1 ) { // ...}

在错误的情况下,你目前只忽略一个
字符。你可能想忽略直到并包括
行尾。
的std :: cin.ignore(的std :: numeric_limits&LT;的std :: streamsize可&GT; :: MAX());

In case of error, you currently only ignore a singlecharacter. You probably want to ignore up to and including theend of line.(std::cin.ignore( std::numeric_limits<std::streamsize>::max() );.

而在你的,如果将​​永远是真实的情况。 (见我
以上版本。)如果你希望找到一个号码是
既不小于也不大于一?

And the condition in your if will always be true. (See myversion above.) Where do you expect to find a number which isneither less than for nor greater than one?

这篇关于你可以解释如何布尔可以控制回路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 03:08