小编Dan*_*ims的帖子

为什么这个for循环不会中断

我正在使用这个资源学习C++ http://www.learncpp.com/cpp-tutorial/58-break-and-continue/

我希望这个程序在输入命中空格后结束并打印空格类型的数量.相反,您可以根据需要输入任意数量的空格.当您按Enter键时,如果空格数超过5,程序将打印1,2,3,4或5.

#include "stdafx.h"
#include <iostream>

int main()
{
    //count how many spaces the user has entered
    int nSpaceCount = 0;
    // loop 5 times
    for (int nCount=0; nCount <5; nCount++)
    {
        char chChar = getchar(); // read a char from user

        // exit loop is user hits enter
        if (chChar == '\n')
            break;
        // increment count if user entered a space
        if (chChar == ' ')
            nSpaceCount++;
    }

    std::cout << "You typed " << nSpaceCount << " spaces" …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop

1
推荐指数
1
解决办法
72
查看次数

标签 统计

c++ ×1

for-loop ×1