完整题目:编写一个程序, 它使用一个 char 数组和循环来每次读取一个单词, 直到用户输入 done 为止。随后, 该程序指出用户输入了多少个单词(不包括 done 在内)。下面是该程序的运行情况:
#include<iostream>
#include<cstring>//使用strcmp()函数
using namespace std;
const int SIZE = 30;//定义每个单词最大的长度常量
const char overflag[] = "done";//定义结束标志位字符常量
int main()
{
int count = 0;//计算读取单词的个数
char words[SIZE];
cout << "Enter words(to stop,type the word done):" << endl;
while (strcmp(overflag, words)!=0)//将输入单词与结束标志对比,等于0表示输入为“done”,非0则不是“done”
{
count++;
cin >> words;
cin.get();//删除空字符
}
cout << "You entered a total of " << count - 1 << " words." << endl;
return 0;
}