|
查看: 1340|回复: 2
|
C++ 问题。。
[复制链接]
|
|
|
请问下各位大大。。为什么当我insert answer第一个字母result就对了?谢谢。。
#include<iostream.h>
#include<fstream.h>
#include<cstring.h>
#include<math.h>
class Hangman
{
private :
char output[50];
char word[50];
char answer[50];
public :
void ReadFile();
void Play();
};
void Hangman::ReadFile()
{ int temp;
int r;
ifstream file;
file.open("Data.txt");
srand(time(0));
r=(rand() %10+1);
for(int i=0; i<r; i++)
file>>output;
strcpy(word,output);
for( int s =0;s<word[s];s++)
{
temp= word[i];
word[i]=word[s];
word[s]=temp;
} file.close(); };
void Hangman:: Play()
{
int i;
int choose,mistakes=0;
int const MAXGUESS=3;
cout<<"\nAfter arranged :\t\t "<<word<<"\n";
while(choose =1 )
{
cout<<"\nYou have" <<" "<<(MAXGUESS-mistakes)<<" "<< "guessing remaining.\n";
if ( MAXGUESS-mistakes !=0)
{ cout<<" \nPlease Enter Your Guess :";
cin>> answer; }
if ( MAXGUESS-mistakes == 0)
{cout<<"\nGame Over\n";
cout<<"\nThe answer is : "<<output;
break; }
if(answer[i] == output[i])
{
cout<<"\nYou Are Right!!";
break; }
else
{ cout<<"\nWrong!!Please try again.\n";
mistakes++;
choose = 1; } }
} ;
void main()
{
Hangman player;
player.ReadFile();
player.Play();
} |
|
|
|
|
|
|
|
|
|
|
发表于 4-4-2011 11:49 PM
|
显示全部楼层
- void Hangman:: Play()
- {
- int i=0;
复制代码 这样就可以了.
不然i, 是未知数, |
|
|
|
|
|
|
|
|
|
|
发表于 13-4-2011 01:00 PM
|
显示全部楼层
本帖最后由 geekman 于 13-4-2011 01:05 PM 编辑
回复 1# sengfei
下次发文附上源代码的时候请善用代码格式:[ code ] [ /code ] 要想别人帮忙,最低限也得别把问题复杂化,不是吗?你的代码没有整齐的格式化,会让人看到一个头两个大,进而没兴趣仔细的帮你。在你的代码里加多几个空白号(space)和新行列号(new line)并不会拖慢你的程式执行速度,但是却能让你的代码更容易阅读,将来在团体合作的工作环境里,将可以让你免于被你愤怒的同事绑在柱子上架起柴火把你烤到脱毛。
我帮你重新格式化,并排列整齐,马上就看到几个问题所在:
- class Hangman
- {
- private :
- char output[50];
- char word[50];
- char answer[50];
- public :
- void ReadFile();
- void Play();
- }; [color=RoyalBlue]《-这个;是必要的,因为这是Class Declaration[/color]
- void Hangman::ReadFile()
- {
- int temp;
- int r;
- ifstream file;
- file.open("Data.txt");
- srand(time(0));
- r=(rand() %10+1); //请确定你是要 (rand() % 10) + 1,还是 rand() % (10 + 1),这会让你的同事们感到疑惑
-
- for(int i=0; i<r; i++)//你确定要用一个Random no 作为你的loop condition? 设计 一个随机/不可确定的写入动作?
- file>>output; //在没有正确的使用{},你的for loop只会对这一行产生作用,也就是说你只是在随机的将file的内容写入output里面,而且很大的可能性会造成overflow
- strcpy(word, output);
- for(int s =0; s<word[s]; s++)
- {
- temp= word[i]; //这里的 i 从那里来?上面的for loop并没有延伸到这里来的哦。
- word[i]=word[s];
- word[s]=temp;
- }
- file.close();
- }; //《-这个;却是不必要的,因为这是Function / method code
- void Hangman:: Play()
- {
- int i;
- int choose, mistakes=0;
- int const MAXGUESS=3;
- cout<<"\nAfter arranged :\t\t "<<word<<"\n";
- while(choose = 1 ) //你确定你的 while loop 要用 variable assignment 作为条件?这将会是一个永远无解的死循环
- {
- cout<<"\nYou have" <<" "<<(MAXGUESS-mistakes)<<" "<< "guessing remaining.\n";
- if ( MAXGUESS-mistakes != 0)
- {
- cout<<" \nPlease Enter Your Guess :";
- cin>> answer;
- }
- if ( MAXGUESS-mistakes == 0) //你还没学会 if()… else 的运用?
- {
- cout<<"\nGame Over\n";
- cout<<"\nThe answer is : "<<output;
- break;
- }
- if(answer[i] == output[i]) //好一个天外飞 i ,无迹可寻。你没有initialize 你的 i ,也没有 increment 你的 i,这个answer[i] == output[i] 到底有何意义?
- {
- cout<<"\nYou Are Right!!";
- break;
- }
- else
- {
- cout<<"\nWrong!!Please try again.\n";
- mistakes++;
- choose = 1;
- }
- //HAL:I'm afraid I can't let you do that, Dave. 你的逻辑完全无法成立。
- }
- } ; //《-这个;却是不必要的,因为这是Function / method code,不过无伤大雅,顶多让你的同事笑掉大牙罢了
- void main()
- {
- Hangman player;
- player.ReadFile();
- player.Play();
- }
复制代码 如果我是你的老师,我会让你去看100遍的 2001: A Space Odyssey,直到你背熟 HAL 9000 的著名台词。
http://www.imdb.com/title/tt0062622/quotes |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|