我正在使用这个资源学习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)