在字符串的开头找到元音和大写字母?

时间:2013-11-21 01:34:10

标签: c++ string function

我一直在做一个记忆游戏作为我正在做的教科书的练习。它被称为奶奶的行李箱,它在一个回合中你在行李箱中找到了一个项目,下一个回合你说你发现了上一个项目和本回合中的最新项目......我想。

大多数情况下,这是一个使用函数的练习,我认为我已经很好地完成了。但我的输出是完全错误的。我相信我已经将问题定位在一个函数中,我应该分析第一个字符并确定它是否需要字符串之前的AN或A或THE。我用来从小型数据库中提取预定义项目的随机函数可能存在问题。 int main()函数应该是相对完整的,这只是一个掌握函数的练习......我,sorta?宁可称之为新手经验。

我想也许是因为我遇到了一个空白行的getline bug,根据我的理解,它是由cin.ignore()修复的;但所有这一切都迫使我在输入数据时按两次输入。哪个......我有点像。也许我正在使用像isupper和.at()错误的小玩意儿?我尝试使用find_first_of但它并没有真正改变任何东西。

输出调用存储主干和主人奶奶,只使用word1 word2 word3 ... wordn ....作为找到的项目留给我输出。

In grandma trunk you've found a
and  an ord3 word1.

它完全混淆了输出。我开始认为我给出的int main()主体并不完全是恒星。但我对自己的文章功能不能100%自信。任何帮助都会令人难以置信。我一直在努力在众多书籍中使用它,并从伙伴的建议中自学一点关于编程。这是一个相当巨大的头痛。

程序本身

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
using namespace std;

string CorrectArticle(string phrase);

string GetPhrase(void);

bool Continue(void);

string UpperCase(string);

string RandomItem(void);

const string PUNCTUATION = ".";

int main(){
   //Variables

   int turn;
   bool flag;
   string phrase,
       article,
       story, item,
       storage, owner;

   srand(time(NULL));

   cout << "Welcome to Grandmother's Trunk 9000" << endl;
   cout << "This is a memory game. Each turn a player" << endl;
   cout << "Says an item to place inside a trunk. " << endl;
   cout << "And the next player has to say what the " << endl;
   cout << "previous player said plus his/her own item." << endl;
   cout << "This will go around in revolving turns." << endl;

   cout << endl << endl;

   cout << "But Grandma's Trunk is a little dry..." << endl;
   cout << "Let's change what the storage is and " << endl;
   cout << "Who owns it." << endl << endl;
   //define storage variable
   cout << "What exactly is this storage?" << endl;
   getline (cin, storage);

   cout << "So the items are stored in " << storage << endl;
   cout << endl;
   //define owner
   cout << "Who owns this " << storage << " ?" << endl;
   getline (cin, owner);

   cout << "The owner is " << owner << endl;

   story = "In "+ owner + " " + storage + " you've found ";

   turn = 0;
   flag = Continue();


   //While flag is true
   while (flag) {
      if (turn %2 == 0) {
            item = GetPhrase();
      } else {
            item = RandomItem();
      }

      //set corrected item to article
      article = CorrectArticle(item);

      //advance the story every item
      story = story + "\n and " + article + " " + item;
      cout  << story  << PUNCTUATION << endl;
      turn++;
      flag = Continue();
   }

   return (0);
}





//Gives A, AN, and THE to correct words
// An if phrase starts with i,e,i,o,u or y
// A if phrase starts with other lower case letters
// The for phrases that start with an uppercase letter
string CorrectArticle(string phrase){

int i=0;

string correctedString;

string stringAn;
string stringA;
string stringThe;

stringAn= " an ";
stringA = " a ";
stringThe= "The ";

if (GetPhrase().at(i) = "a" or "e" or "i" or "u"){

   correctedString = stringAn + GetPhrase();

}else if (isupper(GetPhrase().at(i))){

   correctedString = stringThe + GetPhrase();

}else{
   correctedString = stringA + GetPhrase();
}

return correctedString;

}




//This function takes no parameters
//and returns the user's input
string GetPhrase(void){

   string itemInput;
   cout << "\nWhat did you find? \n" << endl;
   getline (cin, itemInput);

   cout << "\nYou found " << itemInput << endl;
   cin.ignore();
   return itemInput;
}





//Asks user if they wish to continue
bool Continue(void){
//return false if no, true if yes
   string continueString;

   cout << "Would you like to continue?";
   cout << " Yes or No would suffice" << endl;
   getline(cin,continueString);

   UpperCase(continueString);
   cout << "You picked " << continueString;

   if (UpperCase(continueString).find("NO") != string::npos){
      return false;
   } else if (UpperCase(continueString).find("YES") != string::npos){
      return true;
   }
}

//Changes the string to uppercase
string UpperCase(string stringUpper){
   int i = 0;

      while (i<stringUpper.size()){
         stringUpper[i] = toupper(stringUpper[i]);
         i++;
      }
   return stringUpper;
}


//Randomizes items found in game
string RandomItem(void){
   int randomNumber;
   int maxNumberOfItems = 5;

   string randomizedItem;

   randomNumber= rand() % maxNumberOfItems;

   switch (randomNumber){
         case 0:
         randomizedItem = "Smaug";
         break;
      case 1:
         randomizedItem = "Batman";
         break;
      case 2:
         randomizedItem = "Yoda";
         break;
      case 3:
         randomizedItem = "Paul Atreides";
         break;
      case 4:
         randomizedItem = "Captain Kirk";
         break;
      default:
         cout << "ERRORRRR! PANIC!" << endl;
   }
return randomizedItem;
}

1 个答案:

答案 0 :(得分:0)

请注意,=是作业,==用于比较。

还要记住,您必须将变量与值进行比较,例如:

if ((string == "a") or (string == "e") ...

如果or适合您,那就是最好的。我只能使用||。必须是编译器符合性问题。

试试这个:

bool is_vowel(char letter)
{
  const std::string vowels("aeiouAEIOU");
  return (vowels.find_first(letter) != std::string::npos);
}

换句话说,我将所有元音放在一个字符串中搜索字符串。如果匹配,则该字母为元音。