Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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+的编程原理和实践+;而环钻。Can';我找不到退出循环的方法_C++_While Loop_Exit - Fatal编程技术网

C++ 使用C+的编程原理和实践+;而环钻。Can';我找不到退出循环的方法

C++ 使用C+的编程原理和实践+;而环钻。Can';我找不到退出循环的方法,c++,while-loop,exit,C++,While Loop,Exit,我正在学习比亚恩·斯特劳斯塔普的《编程:使用C++的原则和实践》,我有一个练习,它告诉我写一个程序,它由一个while循环组成,该循环(每次循环)读取两个整数,然后打印它们,当输入“|”时,它应该退出。我已经编写了这个程序(我相信有一种更简单的方法可以编写它,但我有一种“天赋”让事情变得过于复杂),但我找不到退出循环的方法。下面是代码: #include <iostream> #include <vector> int main() { std::vector

我正在学习比亚恩·斯特劳斯塔普的《编程:使用C++的原则和实践》,我有一个练习,它告诉我写一个程序,它由一个while循环组成,该循环(每次循环)读取两个整数,然后打印它们,当输入“|”时,它应该退出。我已经编写了这个程序(我相信有一种更简单的方法可以编写它,但我有一种“天赋”让事情变得过于复杂),但我找不到退出循环的方法。下面是代码:

#include <iostream>
#include <vector>

int main()
{

    std::vector<int> answers;
    int answer;
    int intCounter=0;

    while(answers.size()<=2  )
    {

        if(answer=='|')
        {
            return 0;
        }
        std::cin>>answer;
        answers.push_back(answer);
        ++intCounter;

        if(intCounter==2)
        {
            for(int x : answers)
            {
                std::cout<<x<<'\n';
            }
            answers.clear();
            intCounter=0;
        }

    }

    return 0;
}
#包括
#包括
int main()
{
向量答案;
int答案;
int计数器=0;
while(answers.size()>answer;
回答。推回(回答);
++计数器;
if(intCounter==2)
{
对于(int x:答案)
{

当下一个要读取的非空白字符不是数字时,std::cout
cin>>应答将失败。您可以使用该行为结束循环

// Break inside the loop.
while( true  )
{
    std::cin>>answer;

    // If there was an error in reading, break out of the loop.
    if ( !std::cin )
       break;

    answers.push_back(answer);

    ++intCounter;

    if(intCounter==2)
    {
        for(int x : answers)
        {
            std::cout<<x<<'\n';
        }
        answers.clear();
        intCounter=0;
    }
}
//在循环内中断。
while(true)
{
std::cin>>答案;
//如果读取时出错,则中断循环。
如果(!std::cin)
打破
回答。推回(回答);
++计数器;
if(intCounter==2)
{
对于(int x:答案)
{
标准::cout两个变化:

  • 如果您的任务是只打印数字,您可以使用字符串

  • 您正在检查
    |
    答案的值,甚至在输入之前。 所以

    #包括
    #包括
    #包括
    使用名称空间std;
    int main(){
    向量答案;
    字符串回答;
    int计数器=0;
    while(answers.size()>answer;
    //输入后
    如果(答案==“|”){
    返回0;
    }
    回答。推回(回答);
    ++计数器;
    if(intCounter==2){
    对于(int x=0;xstd::couttake在输入中作为
    std::string
    输入,如果不是
    |
    则转换为int,因为在初始化之前使用
    answer
    。未初始化的局部变量有一个不确定的值,在初始化之外使用它们会导致上述未定义的行为。Oh我的天哪,请正确格式化您的代码!:P(1)当您编写
    “|”
    时,它不同于
    “|”
    。第一个是单个字符,可以解释为字母或整数。第二个是字符串,可以与另一个字符串进行比较此外,如果您希望退出循环,但不想退出程序,则可以使用
    break
    。使用
    return
    可以退出while循环及其所在的函数,在本例中是
    main
    ,这将导致整个程序完成。
    #include<iostream>
    #include <vector>
    #include<string>
    using namespace std;
    int main(){
    
    std::vector<string> answers;
    string answer;
    int intCounter=0;
    
    while(answers.size()<=2  ){
    
    std::cin>>answer;
    //after input
    if(answer=="|"){
    return 0;
    }
    answers.push_back(answer);
    ++intCounter;
    
    if(intCounter==2){
    for(int x=0;x< answers.size();x++){
    std::cout<<answers[x]<<'\n';
    }
    answers.clear();
    intCounter=0;
    }
    }
    
    return 0;
    }