佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1340|回复: 2

C++ 问题。。

[复制链接]
发表于 4-4-2011 09:45 PM | 显示全部楼层 |阅读模式
请问下各位大大。。为什么当我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();
}
回复

使用道具 举报


ADVERTISEMENT

发表于 4-4-2011 11:49 PM | 显示全部楼层
  1. void Hangman:: Play()
  2. {
  3. int i=0;
复制代码
这样就可以了.
不然i, 是未知数,
回复

使用道具 举报

发表于 13-4-2011 01:00 PM | 显示全部楼层
本帖最后由 geekman 于 13-4-2011 01:05 PM 编辑

回复 1# sengfei

下次发文附上源代码的时候请善用代码格式:[ code ] [ /code ] 要想别人帮忙,最低限也得别把问题复杂化,不是吗?你的代码没有整齐的格式化,会让人看到一个头两个大,进而没兴趣仔细的帮你。在你的代码里加多几个空白号(space)和新行列号(new line)并不会拖慢你的程式执行速度,但是却能让你的代码更容易阅读,将来在团体合作的工作环境里,将可以让你免于被你愤怒的同事绑在柱子上架起柴火把你烤到脱毛。

我帮你重新格式化,并排列整齐,马上就看到几个问题所在:

  1. class Hangman
  2. {
  3. private :
  4.         char output[50];
  5.         char word[50];
  6.         char answer[50];
  7. public :
  8.         void ReadFile();
  9.         void Play();
  10. }; [color=RoyalBlue]《-这个;是必要的,因为这是Class Declaration[/color]

  11. void Hangman::ReadFile()
  12. {
  13.         int temp;
  14.         int r;
  15.         ifstream file;
  16.         file.open("Data.txt");
  17.         srand(time(0));
  18.         r=(rand() %10+1); //请确定你是要 (rand() % 10) + 1,还是 rand() % (10 + 1),这会让你的同事们感到疑惑
  19.        
  20.         for(int i=0; i<r; i++)//你确定要用一个Random no 作为你的loop condition? 设计 一个随机/不可确定的写入动作?
  21.                 file>>output;  //在没有正确的使用{},你的for loop只会对这一行产生作用,也就是说你只是在随机的将file的内容写入output里面,而且很大的可能性会造成overflow
  22.         strcpy(word, output);
  23.         for(int s =0; s<word[s]; s++)
  24.         {
  25.                 temp= word[i]; //这里的 i 从那里来?上面的for loop并没有延伸到这里来的哦。
  26.                 word[i]=word[s];
  27.                 word[s]=temp;
  28.         }
  29.         file.close();
  30. }; //《-这个;却是不必要的,因为这是Function / method code

  31. void Hangman:: Play()
  32. {
  33.         int i;
  34.         int choose, mistakes=0;
  35.         int const MAXGUESS=3;

  36.         cout<<"\nAfter arranged :\t\t       "<<word<<"\n";
  37.         while(choose = 1 ) //你确定你的 while loop 要用 variable assignment 作为条件?这将会是一个永远无解的死循环
  38.         {
  39.                 cout<<"\nYou have"  <<"  "<<(MAXGUESS-mistakes)<<"  "<<  "guessing remaining.\n";

  40.                 if ( MAXGUESS-mistakes != 0)
  41.                 {
  42.                         cout<<" \nPlease Enter Your Guess :";
  43.                         cin>> answer;
  44.                 }

  45.                 if ( MAXGUESS-mistakes == 0) //你还没学会 if()… else 的运用?
  46.                 {
  47.                         cout<<"\nGame Over\n";
  48.                         cout<<"\nThe answer is : "<<output;
  49.                         break;
  50.                 }

  51.                 if(answer[i] == output[i]) //好一个天外飞 i ,无迹可寻。你没有initialize 你的 i ,也没有 increment 你的 i,这个answer[i] == output[i] 到底有何意义?
  52.                 {
  53.                         cout<<"\nYou Are Right!!";
  54.                         break;
  55.                 }
  56.                 else
  57.                 {
  58.                         cout<<"\nWrong!!Please try again.\n";
  59.                         mistakes++;
  60.                         choose = 1;
  61.                 }
  62. //HAL:I'm afraid I can't let you do that, Dave. 你的逻辑完全无法成立。
  63.         }
  64. } ; //《-这个;却是不必要的,因为这是Function / method code,不过无伤大雅,顶多让你的同事笑掉大牙罢了


  65. void main()
  66. {
  67.   Hangman player;
  68.   player.ReadFile();
  69.   player.Play();
  70. }
复制代码
如果我是你的老师,我会让你去看100遍的 2001: A Space Odyssey,直到你背熟 HAL 9000 的著名台词。
http://www.imdb.com/title/tt0062622/quotes
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-11-2025 03:12 AM , Processed in 0.117912 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表