8.编写一个程序,它使用一个 char 数组和循环来每次读取一个单词,直到用户输入 done 为止。
随后,该程序指出用户输入了多少个单词(不包括done 在内)。下面是该程序的运行情况:
Enter words(to stop,type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
#pragma region 第五章练习8
/*
## 8.编写一个程序,它使用一个 char 数组和循环来每次读取一个单词,直到用户输入 done 为止。
随后,该程序指出用户输入了多少个单词(不包括done 在内)。下面是该程序的运行情况:
Enter words(to stop,type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
*/
#if 1
#include<iostream>
#include <cstring>
using namespace std;
int main()
{
cout << "Enter words (to stop, type the word done): ";
const char* const szDone = "done";
const unsigned uSize = 64;
int cntWord = -1;
char word[uSize];
do {
cin >> word;
++cntWord;
} while (strcmp(szDone, word));
cout << "You entered a total of " << cntWord << " words." << endl;
return 0;
}
#endif
#pragma endregion
这里使用的是cin接受输入,如果是用getline()函数,本程序会变的复杂,要做隔断和判断