注册 登录
编程论坛 C++教室

C++字符数组读取问题

caochuan2000 发布于 2010-06-16 16:10, 752 次点击
教科书上有这样一道题目,有谁能帮我解答下?
编写一个程序,它使用一个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.
3 回复
#2
caochuan20002010-06-16 18:13
有知道的请帮忙看下,谢谢了哈
#3
张丹2010-06-17 10:09
先定义一个字符数组,还有计数器,即计算单词个数,开始给计数器赋值为0。
在do{}while()循环中输入单词,输入一个单词计数器加1,直到用户输入done
最后打印出用户输入的单词个数。
#4
caochuan20002010-06-17 10:55
谢谢楼上的,原来是我想复杂了。。其实很简单的
#include <iostream>
#include <cstring>
int main()
{
    using namespace std;
    char word[30];
    int space=0;
    cout<<"Enter words (to stop,type the word done):"<<endl;
    cin>>word;
    while(strcmp(word,"done"))
    {
        space++;
        cin>>word;
    }
    cout<<"You entered a total of "<<space<<" words.";
}
1