Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
控制台C++输入,比较整数_C++_Visual Studio_Visual Studio 2010_Visual C++ - Fatal编程技术网

控制台C++输入,比较整数

控制台C++输入,比较整数,c++,visual-studio,visual-studio-2010,visual-c++,C++,Visual Studio,Visual Studio 2010,Visual C++,从行中读取并将输入与整数进行比较的最佳方法是什么? 我知道方法,得到,得到,回答。 我应该使用哪一种?为什么 目前,这种方法不起作用,因为当我输入1或2时,它仍然保持不变。您读取的值是正确的。但是,循环条件是错误的;应该是 bool choose() { int answer = 0; while(answer != 1 || answer != 2) { cout << endl << "Do you want to encrypt(en

从行中读取并将输入与整数进行比较的最佳方法是什么? 我知道方法,得到,得到,回答。 我应该使用哪一种?为什么


目前,这种方法不起作用,因为当我输入1或2时,它仍然保持不变。

您读取的值是正确的。但是,循环条件是错误的;应该是

bool choose() {
    int answer = 0;
    while(answer != 1 || answer != 2) {
        cout << endl << "Do you want to encrypt(enter 1) or decrypt(enter 2)?" << endl;
        cin >> answer;
    }
    if(answer == 1) return true;
    return false;
}

使用| |使条件始终为真,因为没有数字同时等于1和2。

您正确读取了值。但是,循环条件是错误的;应该是

bool choose() {
    int answer = 0;
    while(answer != 1 || answer != 2) {
        cout << endl << "Do you want to encrypt(enter 1) or decrypt(enter 2)?" << endl;
        cin >> answer;
    }
    if(answer == 1) return true;
    return false;
}

使用| |使条件始终为真,因为没有数字同时等于1和2。

您的条件不正确。到,您应该使用&&来代替


你的情况不正确。到,您应该使用&&来代替


这段代码是do-while循环的一个很好的候选者,如果您使用它,就不需要初始化变量answer


对。您可以使用while循环而不是do-while。所有你需要添加的如果!cin>>在代码中回答{…}:-

这段代码是do while循环的一个很好的候选者,如果您使用它,就不需要初始化变量answer


对。您可以使用while循环而不是do-while。所有你需要添加的如果!cin>>在代码中回答{…}:-

根据德摩根定律,另一个条件应该是:!answer==1 | | answer==2正确,因为如果变量具有这两个值中的任何一个,则要退出循环。根据DeMorgan定律,另一个条件应该是:!答案==1 | |答案==2对,因为如果变量有这两个值中的任何一个,你就想退出循环。现在我不确定接受哪一个作为答案,人们回答了我的问题,这正是我想要的,但你指出了更多的东西,我非常感谢。@Jaanus:当然,你应该接受我的:P。。因为这段代码比您所写的更加健壮和完整:-哦,我按你说的做了,对,但当我输入word-like测试输入时,它开始疯狂地在DO中运行,甚至没有给我一个输入任何内容的新机会..只是打印出来你想加密输入1吗。。。。。。。。内隐:/PS!它也会每次运行到if循环中,只是不允许我输入任何内容@贾纳斯:哦。。在cin.clear之后,我没有读到小溪里的垃圾。不管怎样,现在看看我的解决方案。现在正确了。让我知道它是否有效:-嗯,首先cin读取了我的单词,但失败了,因为它是字符串。现在你清除旗帜,然后从某处读入一些东西?我原以为cin是用户输入的……现在我不确定该接受哪一个作为答案,人们回答了我的问题,而且完全符合我的要求,但你指出了更多的东西,我对此非常感谢。@Jaanus:当然,你应该接受我的:P。。因为这段代码比您所写的更加健壮和完整:-哦,我按你说的做了,对,但当我输入word-like测试输入时,它开始疯狂地在DO中运行,甚至没有给我一个输入任何内容的新机会..只是打印出来你想加密输入1吗。。。。。。。。内隐:/PS!它也会每次运行到if循环中,只是不允许我输入任何内容@贾纳斯:哦。。在cin.clear之后,我没有读到小溪里的垃圾。不管怎样,现在看看我的解决方案。现在正确了。让我知道它是否有效:-嗯,首先cin读取了我的单词,但失败了,因为它是字符串。现在你清除旗帜,然后从某处读入一些东西?我以为cin是用户输入的。。。
while(answer != 1 && answer != 2) {
int answer; //= 0; no need to initialize!
do {

  cout<<"Do you want to encrypt(enter 1) or decrypt(enter 2)?"<<endl;
  cin >> answer;

}while(answer != 1 && answer != 2);
int answer = 0; 
while(answer != 1 && answer != 2) {

  cout<<"Do you want to encrypt(enter 1) or decrypt(enter 2)?"<<endl;
  if ( !(cin >> answer) )
  {
    cin.clear(); //clear the failure flag if there is an error when reading!
    std::string garbage;
    std::getline(cin, garbage); //read the garbage from the stream and throw it away
  }
}