| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 893 人关注过本帖
标题:以下是统计单词个数代码,运行结果不对,请求解惑!
只看楼主 加入收藏
love编辰
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-7-8
结帖率:0
收藏
已结贴  问题点数:10 回复次数:11 
以下是统计单词个数代码,运行结果不对,请求解惑!
//使用一个char数组和循环来每次读取一个单词,直到用户输入done为止
//然后,该程序指出输入了多少个单词(不包括done在内)
#include <iostream>
#include <cstring>
const int Number = 50;
const int Size = 10;

int main()
{
    using namespace std;
    char words[Number][Size];
    int counts = 0;

    cout << "Enter words (to top,type the word done): \n";

    do
    {
        
        cin >> words[counts];
        counts ++;

    }while(strcmp(words[counts-1],"done") == 0);
    cout << "You entered a total of " << counts << " words.";
    return 0;
}
搜索更多相关主题的帖子: include Enter 单词 统计 
2013-08-29 20:49
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:4 
把错误信息贴上来瞧瞧

思考赐予新生,时间在于定义
2013-08-29 21:39
额外覆盖
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:城市猎人
威 望:6
帖 子:1726
专家分:5757
注 册:2012-9-22
收藏
得分:4 
数量不对是怎么个不对法?多了?少了?还是永远是一个固定数值?

我现在所学的一切都是为了游戏!!!为了游戏,加油!加油!努力!
2013-08-29 22:07
love编辰
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-7-8
收藏
得分:0 
回复 2楼 love云彩
我输入:aa ss dd ff done
统计的单词数始终为1;
好像cin没有读进我输入的字符串
2013-08-29 22:52
love编辰
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-7-8
收藏
得分:0 
回复 3楼 额外覆盖
永远是一个固定值1,请问一下,cin读字符串时,遇到空格就终止了吗
2013-08-29 22:53
额外覆盖
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:城市猎人
威 望:6
帖 子:1726
专家分:5757
注 册:2012-9-22
收藏
得分:0 
是你的循环判断错了


[ 本帖最后由 额外覆盖 于 2013-8-30 08:52 编辑 ]

我现在所学的一切都是为了游戏!!!为了游戏,加油!加油!努力!
2013-08-29 22:58
love编辰
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-7-8
收藏
得分:0 
回复 6楼 额外覆盖
还不行,按回车就直接终止了输入!
2013-08-29 23:13
额外覆盖
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:城市猎人
威 望:6
帖 子:1726
专家分:5757
注 册:2012-9-22
收藏
得分:0 
以下是引用love编辰在2013-8-29 20:49:04的发言:

//使用一个char数组和循环来每次读取一个单词,直到用户输入done为止
//然后,该程序指出输入了多少个单词(不包括done在内)
#include
#include
const int Number = 50;
const int Size = 10;

int main()
{
    using namespace std;
    char words[Number];
    int counts = 0;

    cout << "Enter words (to top,type the word done): \n";

    do
    {
        
        cin >> words[counts];
        counts ++;

    }while(strcmp(words[counts-1],"done") == 0);//这里的判断条件错了,如果你是想输入不为done就继续应该改为!=0
    cout << "You entered a total of " << counts << " words.";
    return 0;
}
       strcmp()函数比较两个字符串  如果小于就返回-1,等于就返回0,大于就返回1
昨晚在ktv手机登陆的  没看仔细!

我现在所学的一切都是为了游戏!!!为了游戏,加油!加油!努力!
2013-08-30 09:01
love编辰
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-7-8
收藏
得分:0 
回复 8楼 额外覆盖
能再帮忙看一个程序吗?大神!


//handling an array of string objects
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
void display(const string sa[],int n);
int main()
{
    string list[SIZE];
    cout << "Enter your " << SIZE << " favorite astronomical sights:\n";
    for(int i = 0;i < SIZE;i++)
    {
        cout << i + 1 << ": ";
        getline(cin,list[i]);
        //getchar();    //如果注释这条语句,为什么输入数据时有些怪,就第一次输入需要按两次回车键
    }
    cout << "Your list:\n";
    display(list,SIZE);
   
    return 0;
}

void display(const string sa[],int n)
{
    for(int i = 0;i < n;i++)
        cout << i + 1 << ": " << sa[i] << endl;
}
2013-08-30 09:48
额外覆盖
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:城市猎人
威 望:6
帖 子:1726
专家分:5757
注 册:2012-9-22
收藏
得分:0 
以下是引用love编辰在2013-8-30 09:48:30的发言:

能再帮忙看一个程序吗?大神!


//handling an array of string objects
#include
#include
using namespace std;
const int SIZE = 5;
void display(const string sa[],int n);
int main()
{
    string list;
    cout << "Enter your " << SIZE << " favorite astronomical sights:\n";
    for(int i = 0;i < SIZE;i++)
    {
        cout << i + 1 << ": ";
        getline(cin,list);
        //getchar();    //如果注释这条语句,为什么输入数据时有些怪,就第一次输入需要按两次回车键
    }
    cout << "Your list:\n";
    display(list,SIZE);//这里传递的值只是一个变量,但你的函数参数是要传递一个数组!
   
    return 0;
}

void display(const string sa[],int n)
{
    for(int i = 0;i < n;i++)
        cout << i + 1 << ": " << sa << endl;
}
getline(cin,list)函数的默认结束符就是回车,下面的getchar()也需要获取一个字符,所以要两个了!

我现在所学的一切都是为了游戏!!!为了游戏,加油!加油!努力!
2013-08-30 10:28
快速回复:以下是统计单词个数代码,运行结果不对,请求解惑!
数据加载中...
 
   



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

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