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

以下是统计单词个数代码,运行结果不对,请求解惑!

love编辰 发布于 2013-08-29 20:49, 893 次点击
//使用一个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;
}
11 回复
#2
love云彩2013-08-29 21:39
把错误信息贴上来瞧瞧
#3
额外覆盖2013-08-29 22:07
数量不对是怎么个不对法?多了?少了?还是永远是一个固定数值?
#4
love编辰2013-08-29 22:52
回复 2楼 love云彩
我输入:aa ss dd ff done
统计的单词数始终为1;
好像cin没有读进我输入的字符串
#5
love编辰2013-08-29 22:53
回复 3楼 额外覆盖
永远是一个固定值1,请问一下,cin读字符串时,遇到空格就终止了吗
#6
额外覆盖2013-08-29 22:58
是你的循环判断错了


[ 本帖最后由 额外覆盖 于 2013-8-30 08:52 编辑 ]
#7
love编辰2013-08-29 23:13
回复 6楼 额外覆盖
还不行,按回车就直接终止了输入!
#8
额外覆盖2013-08-30 09:01
以下是引用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手机登陆的  没看仔细!
#9
love编辰2013-08-30 09:48
回复 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;
}
#10
额外覆盖2013-08-30 10:28
以下是引用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()也需要获取一个字符,所以要两个了!
#11
TonyDeng2013-08-30 10:42
以下是引用额外覆盖在2013-8-30 09:01:01的发言:

       昨晚在ktv手机登陆的  没看仔细!

这人在干什么呐!
#12
额外覆盖2013-08-30 11:46
回复 11楼 TonyDeng
T版 你说ktv能干嘛,出来蹲坑的时候就掏出手机看了看!
1