为什么cin被用作循环条件?

时间:2016-02-27 03:18:06

标签: c++

在我的c ++书中,有这个循环:

AnswerSelectedId = CorrectAnswerId

这似乎永远循环。我无法阻止这个循环。这有什么用途?

2 个答案:

答案 0 :(得分:0)

operator>>将返回流(cin),该流本身可以转换为bool,表示流是否仍然有效。这将循环,直到导致cin的读取失败(文件结束,字母而不是数字等)。

答案 1 :(得分:0)

这类似于:

<head>       
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>About Us-Azure Solutionz</title>

        <environment names="Development">
            <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
            <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/boots‌​trapValidator.css"/>

            <link rel="stylesheet" href="css/ContactUs.css" />
            <link rel="stylesheet" href="~/css/site.css" />
             <link rel="stylesheet" href="css/NavbarButton.css"/>
            <link rel="stylesheet"  href="css/AboutUs.css"/>

              <link rel="stylesheet" href="css/Footer.css"/>
        </environment>
        <environment names="Staging,Production">
            <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/css/bootstrap.min.css"
                  asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
                  asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
            <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true"/>
        </environment>

    </head>
<environment names="Development">
            <script src="~/lib/jquery/dist/jquery.js"></script>
            <script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
            <script src="~/js/site.js" asp-append-version="true"></script>
        </environment>
        <environment names="Staging,Production">
            <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
                    asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                    asp-fallback-test="window.jQuery">
            </script>
            <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
            <script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/bootstrap.min.js"
                    asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
                    asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal">
            </script>
            <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstr‌​apValidator.js"></script>
            <script src="js/ContactUs.js" type="text/javascript"></script>
            <script src="~/js/site.min.js" asp-append-version="true"></script>
        </environment>

在哪里连续读取数字并打印出来。输入一个非数字会导致输入流失败,因此cin.good()变为false。(流不再好)