将玩家输入与单词文件进行比较

时间:2015-01-27 21:14:18

标签: c++

学生程序员寻求帮助。程序的Jist是接受用户字符输入,将其与合法三字母单词的文件进行比较。必须输出屏幕用户首先输入的内容,当然还要调用功能内的多个功能。从我上一期课程(海外出差)开始已经一年多了,所以我失去了以前收集的一点点知识。如果有人可以查看我的代码并告诉我,我是在正确的道路上还是在路上,我将非常感激。具体来说,我如何测试玩家是否输入小写字母以及如何将玩家字母与合法字词文件进行比较?如果教得正确,我会很快学会。准备好红色墨水。     #包括     #include //需要这个用于字符串类     #include //必须使用它来处理文件

using namespace std;

//Declare Functions before the main??
char menu(); //function prototype for menu function
void processUserInput(); //function prototype for user input function
string findWords(string);
void readLegalWords();


int main()
{
cout<<"Welcome to Jellos TLW Game"<<endl;
char menuChoice = ' '; //variable to hold user's choice

//Call to menu function
menu();

system ("Pause");
return 0;
}

//开始函数定义    //从菜单功能开始提示游戏玩家

char menu()
{
char menuChoice = ' '; //variable to hold choice
cout<<"Enter your Choice: "<<endl;
cout<<"    (F)ind Words"<<endl;
cout<<"    (Q)uit"<<endl;
cin>>menuChoice;

    if(menuChoice == 'F' || menuChoice == 'f')
    {
        /*call to function that will ask user to enter
        between 3-10 letters*/
        processUserInput();

    }
    else if(menuChoice == 'Q' || menuChoice == 'q')
    {
        cout<<"Thanks for playing Jellos TLW Game!"<<endl;
        return 0;
    }
} //end of menu function


void processUserInput(char); //function definition
{
cout<<"Enter up to 10 lowercase letters."<<endl;
cout<<"You must enter at least 3 letters."<<endl;
cout<<"Enter a '-' to stop reading letters if you want less than  
10."<<endl;

//variable to hold string of letters
string inputLetters;
//read the letters
cin>>inputLetters;

if(inputLetters.size() >= 3 && inputLetters.size() <= 10 && inputLetters 
== '-')

   {    //display what the player typed in to screen on one line
    cout<<"you entered the following letters."<<inputLetters<<endl;
    //call to function to test the letters against legal 3 letter word 
 file
            //read into an array of legal 3-letter words.
    //

    findWords(string);//function call
    }

    else if(letters.size() < 3 && letters.size() > 10) //test the input
     {
         cout<<"Please enter between 3-10 lowercase letters."<<endl;
     }
 }


/* Test the following conditions are met
** at least 3 letters are entered
** no more than 10 letters are entered
** so long as 3 letters are entered a - will stop input
** all input is lowercase letter or - anything else will display error  
message

** once input is entered correctly the letters the player entered are 
displayed
back to him/her on a single line

** correct data will call to function findWords() which will fill an 
array with 
legal 3 letter words from player input and print the words if there are 
more than 1 

** findWords() will most likely have to compare string arrays (player 
input vs file that 
is read from another function */



//Need to test three conditions
//If player entered 3-10 letters
//If player entered lowercase letters
//do not know how to test whether player entered lower case letters

1 个答案:

答案 0 :(得分:0)

我建议您使用toupper()tolower()将字符转换为大写或小写,这样您只需进行一次比较。

其次,如果文件不是非常庞大,请将单词读入字符串向量,例如std::vector<string>

输入播放器字符串。 使用==!=迭代向量,将播放器的文本与文件中的单词进行比较。

一个选项是对矢量中的单词进行排序,并使用类似std::lower_bound的内容来查找单词。如果向量已排序,则不必搜索整个内容。