| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 947 人关注过本帖
标题:用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后统计字 ...
只看楼主 加入收藏
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 10楼 fl8962
因为你获取数目后,文件已经读完了
程序代码:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    const char* filename = "dictionary";
    ifstream infile( filename );
    if( !infile )
    {
        cerr << "Can't open this dictionary.\n";
        return 1;
    }

    // 输入
    size_t num = 0;
    vector<string> words;
    for( string word; infile>>word; )
    {
        ++num; // 其实根本不需要,因为数量可以从 words.size() 获得
        words.push_back( word );
    }

    // 输出
    for( size_t i=0; i!=words.size(); ++i )
        cout << words[i] << '\n';
    cout << "There are " << num << " words in this dictionary." << endl;

    return 0;
}
或者
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    const char* filename = "dictionary";
    ifstream infile( filename );
    if( !infile )
    {
        cerr << "Can't open this dictionary.\n";
        return 1;
    }

    // 输入
    vector<string> words = vector<string>( istream_iterator<string>(infile), istream_iterator<string>() );

    // 输出
    copy( words.begin(), words.end(), ostream_iterator<string>(cout,"\n") );
    cout << "There are " << words.size() << " words in this dictionary." << endl;

    return 0;
}
2013-11-21 14:34
a190205460
Rank: 2
来 自:湖南
等 级:论坛游民
帖 子:29
专家分:59
注 册:2013-10-26
收藏
得分:6 
很好,很强大
2013-11-21 19:22
帅mmmmmm哥
Rank: 1
等 级:新手上路
帖 子:6
专家分:6
注 册:2013-11-3
收藏
得分:6 
你是用什么来写游戏的?visual c++ 那个?怎么我写的都是黑白程序的。。。求解。。
2013-11-24 10:50
快速回复:用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后 ...
数据加载中...
 
   



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

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