师兄们~C++怎样读TXT檔

noobguy 2008-12-16 04:53:37
师兄们
我想我的HANGMAN里面的生字是读TXT檔的
要怎么做呢?
同学给了我一个例子
但是我做不出呢...
求各位师兄们指教小弟

我的HANGMAN
#include <stdafx.h>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>


using namespace std;


int main() {
printf("Hangman game :-)\n-----------------------\n");
printf("How to play:\n"
"the challenger will put a word in h.txt in the same\n"
"directory as this application. After that run the program!\n\n");
ifstream fp("h.txt");
char s[20];fp >> s;
int n=strlen(s);int i,chances=10;char c,dd=s[0];
char a[20];char d[26];d[0]=0;int j=0,flag=0;
for(i=0;i<n;i++)a[i]='_'; a[n]='\0';
while(chances > 0) {
flag=1;
for(i=0;i<n;i++)printf("%c ",a[i]);
for(i=0;i<n;i++) {
if(a[i] != s[i]) flag=0;
}

{
string playerLetters;

random = (rand()%5)+1;
cout <<"*******\n";
cout <<"* *\n";
cout <<"* \n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
answer = wordBank[random];
string answer1=answer;
string playerWord(answer.size(),'-');
unsigned int tries = 0;
string alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
string anotheralpha;
do
{
cout << "\n\nLetters used : " << playerLetters << "\n\n";
cout << "\n\nUnused letters : " << alphabet << "\n\n";
cout << "\n\nAnswer: " << playerWord << "\n\n";
cout << "\n\nPlease enter a guess: " << "\n\n";
cin >> guess;
if (guess.length() > 1)
{
cout << "\n\nYou entered too many letters.\n\n";
tries = 5;
break;
}

anotheralpha.insert(0, guess);
if(anotheralpha.find(guess, 0)== string::npos) {
alphabet.erase(alphabet.find(guess, 0), 1);
}
if (answer.find(guess, 0)!= string::npos)
{
cout <<"\n\nRIGHT!!!\n\n";
int i = answer1.find(guess, 0);
playerWord.erase(i,1);
playerWord.insert(i,guess);
answer.erase((answer.find(guess, 0)), 1);
playerLetters += guess;
}
else
{
tries++;
cout <<"\n\nWRONG!\n\n";
playerLetters += guess;
if (tries == 1)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 2)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* | \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 3)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -| \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 4)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 5)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | \n";
cout <<"========\n";
}
if (tries == 6)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | |\n";
cout <<"========\n";
}
}

sort(playerLetters.begin(), playerLetters.end());
} while (tries < 6 && (answer.length() != 0));
playerLetters.erase(0, playerLetters.length());
if (tries >= 5)
{
cout << "\n\nYou dead!!!\n";
cout << "\n\nThe answer is: " << wordBank[random] <<"\n\n";
}
else
{
cout <<"\n\nYou won!You are powerful!!!\n\n";
cout <<"\n\nThe answer is: " << wordBank[random] << "\n\n";
}
random = 0;
tries = 0;
cout << "\n\nPlay again(y/n)?";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\n\nThanks for playing!\n\n";
return 0;
}


读TXT檔的HANGMAN[不完整]
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
int main() {
printf("Hangman game :-)\n-----------------------\n");
printf("How to play:\n"
"the challenger will put a word in h.txt in the same\n"
"directory as this application. After that run the program!\n\n");
ifstream fp("h.txt");
char s[20];fp >> s;
int n=strlen(s);int i,chances=10;char c,dd=s[0];
char a[20];char d[26];d[0]=0;int j=0,flag=0;
for(i=0;i<n;i++)a[i]='_'; a[n]='\0';
while(chances > 0) {
flag=1;
for(i=0;i<n;i++)printf("%c ",a[i]);
for(i=0;i<n;i++) {
if(a[i] != s[i]) flag=0;
}

if(flag) break;
flag=0;
printf("used %s chances %d enter>",d,chances);
gets(&c);
if(strchr(d,c)!= NULL) continue; // already entered in missed.
s[0]=dd;
for(i=0;i<n;i++) {
if(s[i]==c ){ a[i]=c;flag=1;}
}

d[j++]=c;d[j]='\0';if(flag==0)chances--;
}
if(flag) printf("you won!\n"); else printf("you lost! the word was %s\n",s);
}

...全文
256 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bfhtian 2008-12-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ztz0223 的回复:]

这样,把你定义文件fp的地方修改一下:

C/C++ code ifstream fp("h.txt");
string wordBank[6];
int i = 1;
while(i <= 6) {
fp >> wordBank[i];
i++;
}



就是从文件里面读书你要的单词了,试试
[/Quote]
up,这样就可以了,楼主可以试试
noobguy 2008-12-16
  • 打赏
  • 举报
回复
#include <stdio.h> 
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>


using namespace std;


int main()
{
char again;
string guess;
string s;
ifstream fp("h.txt");
string wordBank[6];
int i = 1;
while(i <= 6) {
fp >> wordBank[i];
i++;
string answer;
int random = 0;
unsigned int i = 0;
srand((unsigned)time(0));
cout << "\n\nLet's play Hangman!\n\n";

{
string playerLetters;
random = (rand()%5)+1;
cout <<"*******\n";
cout <<"* *\n";
cout <<"* \n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
answer = wordBank[random];
string answer1=answer;
string playerWord(answer.size(),'-');
unsigned int tries = 0;
string alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
string anotheralpha;
do
{
cout << "\n\nLetters used : " << playerLetters << "\n\n";
cout << "\n\nUnused letters : " << alphabet << "\n\n";
cout << "\n\nAnswer: " << playerWord << "\n\n";
cout << "\n\nPlease enter a guess: " << "\n\n";
cin >> guess;
if (guess.length() > 1)
{
cout << "\n\nYou entered too many letters.\n\n";
tries = 5;
break;
}

anotheralpha.insert(0, guess);
if(anotheralpha.find(guess, 0)== string::npos) {
alphabet.erase(alphabet.find(guess, 0), 1);
}
if (answer.find(guess, 0)!= string::npos)
{
cout <<"\n\nRIGHT!!!\n\n";
int i = answer1.find(guess, 0);
playerWord.erase(i,1);
playerWord.insert(i,guess);
answer.erase((answer.find(guess, 0)), 1);
playerLetters += guess;
}
else
{
tries++;
cout <<"\n\nWRONG!\n\n";
playerLetters += guess;
if (tries == 1)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 2)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* | \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 3)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -| \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 4)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* \n";
cout <<"========\n";
}
if (tries == 5)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | \n";
cout <<"========\n";
}
if (tries == 6)
{
cout <<"*******\n";
cout <<"* *\n";
cout <<"* O\n";
cout <<"* -|- \n";
cout <<"* | |\n";
cout <<"========\n";
}
}

sort(playerLetters.begin(), playerLetters.end());
} while (tries < 6 && (answer.length() != 0));
playerLetters.erase(0, playerLetters.length());
if (tries >= 5)
{
cout << "\n\nYou dead!!!\n";
cout << "\n\nThe answer is: " << wordBank[random] <<"\n\n";
}
else
{
cout <<"\n\nYou won!You are powerful!!!\n\n";
cout <<"\n\nThe answer is: " << wordBank[random] << "\n\n";
}
random = 0;
tries = 0;
cout << "\n\nPlay again(y/n)?";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\n\nThanks for playing!\n\n";
return 0;
}
}


师兄们
这样对吗?
但是好像有问题的样子
谢谢
jieao111 2008-12-16
  • 打赏
  • 举报
回复
读文件都不会,就没什么办法了
lgccaa 2008-12-16
  • 打赏
  • 举报
回复
up
nullah 2008-12-16
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string s;
ifstream dict("aaa.txt");
//读出文件中的单词
while (dict>>s)
{
cout << s <<"\n";
}
dict.close();

fstream out("aaa.txt",ofstream::app);
s = "xxxx";
out << s << endl;//往文件中写入xxxx

}
就呆在云上 2008-12-16
  • 打赏
  • 举报
回复

这样,把你定义文件fp的地方修改一下:
    ifstream fp("h.txt");
string wordBank[6];
int i = 1;
while(i <= 6) {
fp >> wordBank[i];
i++;
}


就是从文件里面读书你要的单词了,试试
nullah 2008-12-16
  • 打赏
  • 举报
回复

ifstream fp("h.txt");//楼主这不是写了的嘛 用文件流绑定到txt文件 就OK
char s[20];
fp >> s;//在这里写

//同样输出流
ofstream b("h.txt");
内容概要:本文围绕“新型电力系统下多分布式电源接入配电网承载力评估方法”的研究,系统性地介绍了基于Matlab的仿真建模与代码实现方案,旨在评估高比例分布式电源(如光伏、风电等)接入背景下配电网的接纳能力。研究融合了智能优化算法(如蜣螂优化、灰狼优化、遗传算法)、多目标优化、鲁棒优化及双层优化模型,结合潮流计算、稳定性分析与故障仿真,构建了完整的承载力评估体系。文档不仅提供核心算法实现,还拓展至微电网调度、储能配置、电氢耦合系统、电动汽车协同等前沿方向,强调“复现+创新”相结合的科研路径,助力研究者快速掌握高水平论文复现技巧并激发原创思路。; 适合人群:具备电力系统、自动化或相关专业背景,熟悉Matlab/Simulink仿真环境,正在从事科研或工程应用的研究生及初级科研人员(工作1-3年);; 使用场景及目标:①复现高水平期刊中关于配电网承载力的优化模型;②开展高比例可再生能源接入下的配电网规划与运行研究;③学习并应用智能优化算法解决复杂电力系统问题;④获取完整科研资源包以加速课题进展与论文撰写; 阅建议:建议者关注公众号“荔枝科研社”获取网盘资源,下载全套代码与模型文件,按照文档结构循序渐进学习,重点理解算法设计逻辑与仿真建模细节,结合所提供的复现案例深化对优化模型与工程应用场景的理解,提升科研效率与创新能力。

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧