下面是两种方式的实现,部分代码:
C style strcmp函数:
const int STR_LIM = 50;
int n = 0;
char word[STR_LIM];
cout << "Enter words (to stop, type the word \"done\"):\n";
while (cin >> word && strcmp(word, "done"))
{
++n;
};
cout << "You entered " << n << " words." << endl;
C++ class:
int n = 0;
string word;
cout << "Enter words (to stop, type the word \"done\"):\n";
while (cin >> word && (word != "done"))
{
++n;
};
cout << "You entered " << n << " words." << endl;
当使用class 时不能使用strcmp函数,因为strcmp函数定义的是两个字符串比较,否则会报错。