简体   繁体   English

使用C ++循环钻取的编程原理和实践。 找不到退出循环的方法

[英]Programming Principles and Practice using C++ while-loop drill. Can't find a way to exit the loop

I'm going through Bjarne Stroustrup's 'Programming : Principles and Practice using C++" and I've got a drill which tells me to write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them and that it should exit when "|" is entered. I've written the program(I'm sure there's a easier way of writing it, but I've got the "gift" of overcomplicating things), but I can't find a way of exiting the loop. Here's the code: 我正在阅读Bjarne Stroustrup的“编程:使用C ++的原理和实践”,并且有一个演练告诉我编写一个程序,该程序包含一个while循环,该循环(每次循环)读入两个int,然后然后打印它们,并在输入“ |”时退出。我已经编写了程序(我相信有一种更简单的编写方法,但是我有使事情复杂化的“礼物”),但是我无法找到退出循环的方法,下面是代码:

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

Basically if I write an if statement to check if answer is equal to '|', the compiler thinks i meant the int value of it(124 or something like that) and terminates the loop when I write 124, and that's clearly not what I want. 基本上,如果我编写一条if语句来检查答案是否等于“ |”,则编译器认为我的意思是它的int值(124或类似的值),并在我写124时终止循环,这显然不是我所要的。想。 I've tried to look over the internet for a way of converting an int into a char, but I haven't understood anything from there. 我试图在互联网上寻找一种将int转换为char的方法,但是我对此一无所知。 The simplest solution would be the best. 最简单的解决方案是最好的。

cin >> answer will fail when the next non-whitespace character to be read is not a number. 如果要读取的下一个非空白字符不是数字,则cin >> answer将失败。 You can use that behavior to end the loop. 您可以使用该行为结束循环。

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

Two changes: 两项更改:

  1. If your task is to just print numbers you could just use string. 如果您的任务只是打印数字,则可以只使用字符串。

  2. You are checking value of answer to | 您正在检查答案的值| before its even been input. 甚至没有输入。 So 所以

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM