首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将播放器输入与单词文件进行比较

将播放器输入与单词文件进行比较
EN

Stack Overflow用户
提问于 2015-01-28 05:14:18
回答 1查看 132关注 0票数 0

学生程序员正在寻找作业的帮助。Jist的程序是接受用户输入的字符,将其与文件中的合法三个字母的单词进行比较。必须先将用户输入的内容输出到屏幕上,当然还需要调用函数中的多个函数。距离我上一次课程(海外工作旅行)已经过去一年多了,所以我失去了之前学到的一点知识。如果有人能看看我的代码,告诉我我是在正确的道路上还是走错了路,我将不胜感激。具体地说,我如何测试玩家是否输入了小写字母,以及如何将玩家字母与合法单词文件进行比较?如果教得正确,我学得很快。准备好红色墨水了。#include #include //字符串类需要这个#include //必须有这个才能处理文件

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-01-28 08:22:53

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

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28180120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档