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

用C++ 写游戏hangman出现了一些问题希望大家可以指点一下(文件读入后统计字符串个数出现问题)

fl8962 发布于 2013-11-21 08:47, 947 次点击
#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;
}
12 回复
#2
rjsp2013-11-21 09:31
不知所云
你想实现什么功能?你遇到什么问题?你代码中前段和后段有什么关联?
#3
rjsp2013-11-21 09:39
我猜你想要的是

程序代码:
#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;
}

#4
fl89622013-11-21 11:38
回复 3楼 rjsp
while (true) {
  getline (infile, line);
  if (infile.eof()) break;
  cout << line << endl;
}
不好意思啊,文件的输入和输出我是自学,也没人教。我是根据书上的程序自己写的,书上说while(true)是无穷循环的习惯写法。通常循环中某处会有个break语句,这样程序就不会真的永远运行下去,然后在这个while循环里放一个 eof 函数来探测”文件尾“ 然后用break 退出循环。
#5
fl89622013-11-21 11:41
回复 3楼 rjsp
就是我这个程序里需要用到一个dictionary 的文件,这个文件里面有很多歌单词,但是我不知道有多少个,我就是想知道里面有多少个单词。。。
#6
fl89622013-11-21 11:54
回复 3楼 rjsp
按照你给的代码,我成功找出了我这个文件里面的单词数目,想请问下如何把这个文件里的单词一个一个以字符串的形式赋给一个字符串数组呢?我的意思是比如这里有100个单词,我定义一个string word[100] 然后第一个单词赋给word[0],第二个单词赋给word[1]。 谢谢
#7
rjsp2013-11-21 12:08
回复 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;
}

#8
fl89622013-11-21 12:30
回复 7楼 rjsp
谢谢啊,我在美国读计算机,因为我英语不太好,所以每次问老师都问不清楚,真的十分感谢,谢谢你的帮助。
#9
fl89622013-11-21 12:32
回复 7楼 rjsp
string words[24000];
   for(int i=0;i<23718;++i)
   {infile>>words[i];}
还有我这样写为什么不可以呀
#10
fl89622013-11-21 12:49
回复 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 请问这是什么原因?谢谢。
#11
rjsp2013-11-21 14:34
回复 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;
}
#12
a1902054602013-11-21 19:22
很好,很强大
#13
帅mmmmmm哥2013-11-24 10:50
你是用什么来写游戏的?visual c++ 那个?怎么我写的都是黑白程序的。。。求解。。
1