我学习C++使用这个资源http://www.learncpp.com/cpp-tutorial/58-break-and-continue/为什么for循环不打破
我希望这个节目结束并打印空间类型的数量被输入命中空间之后。相反,您可以根据需要输入尽可能多的空格。当您按回车键时,如果空格数超过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" << std::endl;
std::cin.clear();
std::cin.ignore(255, '/n');
std::cin.get();
return 0;
}
我现在明白了。该程序从用户处获取一个字符串,并逐字读取,直到达到输入符号或读取了5个空格。我是小白。 – 2014-12-03 21:37:46
@DanielSims,也许你是一个noob,但它根本不是一个愚蠢的问题。有时机罩下的机械装置不明显。 – 2014-12-03 21:42:33