| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 947 人关注过本帖
标题:用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后统计字 ...
只看楼主 加入收藏
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
结帖率:96.23%
收藏
已结贴  问题点数:18 回复次数:12 
用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后统计字符串个数出现问题)
#include<iostream>
#include<ncurses.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
   char a,h,b,d;
   a='/';
   h='o';
   b='|';
   d='-';
   int row,col;
   initscr();
char line[9][10]={
   "  _______",
   "  |     |",
   "  |      ",
   "  |      ",
   "  |      ",
   "|_|______",
   "|       |",
   "|_______|",};
   int i,j,n=0;
for(i=0;i<=7;++i)
 {
    for(j=0;j<=8;++j)
     {  mvprintw(5+i,3+j,"%c",line[i][j]);
     }
 }

   refresh();
   getch();
   endwin();
  string fileName = "dictionary";
  ifstream infile(fileName.c_str());
   if(infile.good()==false)
   cout<<" can't open this dictionary."<<endl;
   while(true)  //这里我想用while循环得到dictionary 文件中的单词个数
 {
     n++;
     if (infile.eof())//当读入到文件尾的时候跳出循环
     break;
   }
   infile.close();
   cout<<"there are "<<n<<"words in this dictionary"<<endl;
   return 0;
}
搜索更多相关主题的帖子: include 字符串 统计 游戏 
2013-11-21 08:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:6 
不知所云
你想实现什么功能?你遇到什么问题?你代码中前段和后段有什么关联?
2013-11-21 09:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
我猜你想要的是

程序代码:
#include <iostream>
#include <fstream>
#include <string>
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;
    for( string word; infile>>word; )
        ++num;

    cout << "There are " << num << " words in this dictionary." << endl;

    return 0;
}

2013-11-21 09:39
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 3楼 rjsp
while (true) {
  getline (infile, line);
  if (infile.eof()) break;
  cout << line << endl;
}
不好意思啊,文件的输入和输出我是自学,也没人教。我是根据书上的程序自己写的,书上说while(true)是无穷循环的习惯写法。通常循环中某处会有个break语句,这样程序就不会真的永远运行下去,然后在这个while循环里放一个 eof 函数来探测”文件尾“ 然后用break 退出循环。

想抽苏烟了。
2013-11-21 11:38
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 3楼 rjsp
就是我这个程序里需要用到一个dictionary 的文件,这个文件里面有很多歌单词,但是我不知道有多少个,我就是想知道里面有多少个单词。。。

想抽苏烟了。
2013-11-21 11:41
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 3楼 rjsp
按照你给的代码,我成功找出了我这个文件里面的单词数目,想请问下如何把这个文件里的单词一个一个以字符串的形式赋给一个字符串数组呢?我的意思是比如这里有100个单词,我定义一个string word[100] 然后第一个单词赋给word[0],第二个单词赋给word[1]。 谢谢

想抽苏烟了。
2013-11-21 11:54
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
回复 6楼 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;
    }

    // 输入
    vector<string> words;
    for( string word; infile>>word; )
        words.push_back( word );

    // 输出
    for( size_t i=0; i!=words.size(); ++i )
        cout << words[i] << '\n';
    cout << flush;

    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 << flush;

    return 0;
}

2013-11-21 12:08
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 7楼 rjsp
谢谢啊,我在美国读计算机,因为我英语不太好,所以每次问老师都问不清楚,真的十分感谢,谢谢你的帮助。

想抽苏烟了。
2013-11-21 12:30
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 7楼 rjsp
string words[24000];
   for(int i=0;i<23718;++i)
   {infile>>words[i];}
还有我这样写为什么不可以呀

想抽苏烟了。
2013-11-21 12:32
fl8962
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:539
专家分:2471
注 册:2012-10-17
收藏
得分:0 
回复 7楼 rjsp
for(string w;infile>>w;)
    n++;
    cout<<n<<endl;
    cout<<flush;
   vector<string> words;
    for( string word; infile>>word; )
        words.push_back( word );
    cout<<words[1]<<endl;
我把你写的那个统计单词个数的代码加进去了,但是加进去那个统计个数的代码后就出现了Segmentation fault 请问这是什么原因?谢谢。

想抽苏烟了。
2013-11-21 12:49
快速回复:用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后 ...
数据加载中...
 
   



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

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