2015-01-27 2 views
0

ученик программист ищет помощь с назначением. Jist программы должен принимать пользовательский ввод символов, сравнивать его с файлом легальных трехбуквенных слов. Необходимо вывести на экран, что пользователь ввел первым и, конечно, вызывать несколько функций внутри функций. Прошло более года с момента моего последнего курса (рабочая поездка за границу), поэтому я потерял то, что раньше было известно. Было бы признательно, если бы кто-то мог посмотреть на мой код и сказать мне, если я на правильном пути или в пути. В частности, как я могу проверить, если игрок набрал строчные буквы и как мне сравнить буквы игрока с файлом юридических слов? Я учился быстро, если учил правильно. Готов к красным чернилам. #include #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 

ответ

0

Я предлагаю вам использовать toupper() или tolower(), чтобы преобразовать символ в верхний регистр или в нижний регистр, чтобы вы сравнили только один.

Во-вторых, если файл не является чрезвычайно огромным, прочитайте слова в вектор строк, например. std::vector<string>.

Введите строку игрока. Итерации через вектор, используя == или !=, чтобы сравнить текст игрока с словами из файла.

Опция состоит в том, чтобы сортировать слова в векторе и использовать что-то наподобие std::lower_bound, чтобы найти слова. Если вектор отсортирован, вам не нужно искать все содержимое.