师兄们~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);
}

...全文
255 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");
随着通信电子技术的迅速发展,信息技术给家居行业产生了深远的影响,家居环境的智能化监控已经成为智能家居的一个重要的发展方向。人们逐渐对自己的生活提出一种更高的要求,他们需要一种智能化、可交互,并且融合现代创新科技的产品来改善他们的生活环境,使他们生活更加安全、舒适、便捷、智能。本文根据智能家居的发展背景和研究现状,并且从实用性和可行性角度出发,研究设计了一种基于STM32单片机的智能家居系统。该系统由一个多功能综合的技术系统组成,各个多功能子系统间具有协同配合能力。基于STM32单片机实现的功能子系统包括:智能温度检测,智能湿度检测,智能烟雾/火灾检测智能检测,无线传输,人机交互机构,风扇调节,报警模块。 整个系统分为前端51单片机采集板和后端STM32单片机接收板。 采集板使用DHT11温湿度传感器、MQ烟雾传感器完成室内家居环境的采集。然后通过nRFL24L01将采集到的数据发送给后端。接收板使用LCD1602完成数据显示、使用蜂鸣器模块报警,使用风扇驱动模块调节室内家居环境。本文完成了智能家居控制系统前端、后端软硬件的设计,使用Altium designer绘制了电路原理图,使用Keil C完成了51单片机和STM32单片机的编程与调试。 最后,对本文设计的基于STM32的智能家居控制系统进行部署调试。试验结果表明,该系统可成功应用在智能家居环境检测调节和火灾安全防护的领域,可提高家居生活智能化水平。

65,210

社区成员

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

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