0
嗨,我是一個begginer,我正在閱讀「Beggining C++ through game programming」(第3版)。這實際上是來自書籍,每一行的代碼,並且我在標題中遇到錯誤。上線路24(串theHint = HINT [選擇] [HINT]; //對於字HINT)在CodeBlocks錯誤中得到這個錯誤:數組下標的無效類型'main():: fields [int]'
我使用的碼塊
#include<iostream>
#include<cstdlib>
#include<string>
#include<ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS=5;
const string WORDS[NUM_WORDS][NUM_FIELDS]=
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses","These might help you see the answer."},
{"labored","Going slowly, is it?"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about"}
};
srand(static_cast<unsigned int>(time(0)));
int choice=(rand()% NUM_WORDS);
string theWord=WORDS[choice][WORD]; //WORD TO GUESS
string theHint=HINT[choice][HINT]; //HINT FOR WORD
string jumble=theWord; //jumbled version of word
int lenght=jumble.size();
for(int i=0;i<lenght;++i)
{
int index1=(rand()%lenght);
int index2=(rand()%lenght);
char temp=jumble[index1];
jumble[index1]=jumble[index2];
jumble[index2]=temp;
}
//WELCOMING THE PLAYER
cout<<"\t\t\tWelcome to Word Jumble!\n\n";
cout<<"Unscramble the letters to make a word.\n";
cout<<"Enter 'hint' for hint.\n";
cout<<"Enter 'quit' to quit the game.\n\n";
cout<<"The jumble is: "<<jumble;
string guess;
cout<<"\n\nYour guess: ";
cin>>guess;
//ENTERING THE GAME LOOP
while((guess !=theWord)&& (guess!="quit"))
{
if(guess=="hint")
{
cout<<theHint;
}
else
{
cout<<"Sorry that's not it.";
}
cout<<"\n\nYour guess: ";
cin>>guess;
}
if(guess==theWord)
{
cout<<"\nThat's it! You guessed it!\n";
}
cout<<"\nThanks for playing. \n";
return 0;
}