c++ 猜单词游戏

来源:百度知道 编辑:UC知道 时间:2024/06/04 21:51:16
请问各位高手
如何实现从txt文件中读取多个单词,分别存入字符串数组
再用随机数挑选一个出来~

#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;

int main()
{
ifstream in("a.txt"); //a.txt单词文件
string strtmp;
vector<string> vect;
while(getline(in, strtmp, ' ')) //读入空格分隔的单词
vect.push_back(strtmp);
srand(time(NULL));
int k = rand() % vect.size();
cout<<vect[k]<<endl;
return 0;

};

用vector<string>来保存单词,读文件直接用文件流>>即可

有专门的FSO对象