标题:C++字符数组读取问题
只看楼主
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
结帖率:76.47%
 问题点数:0 回复次数:3 
C++字符数组读取问题
教科书上有这样一道题目,有谁能帮我解答下?
编写一个程序,它使用一个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.
搜索更多相关主题的帖子: 字符 
2010-06-16 16:10
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
得分:0 
有知道的请帮忙看下,谢谢了哈
2010-06-16 18:13
张丹
Rank: 2
等 级:论坛游民
帖 子:94
专家分:66
注 册:2010-6-7
得分:0 
先定义一个字符数组,还有计数器,即计算单词个数,开始给计数器赋值为0。
在do{}while()循环中输入单词,输入一个单词计数器加1,直到用户输入done
最后打印出用户输入的单词个数。
2010-06-17 10:09
caochuan2000
Rank: 2
等 级:论坛游民
帖 子:66
专家分:35
注 册:2009-4-11
得分:0 
谢谢楼上的,原来是我想复杂了。。其实很简单的
#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.";
}
2010-06-17 10:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-310397-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.116062 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved