我一直在做一個小記憶遊戲作爲練習從我正在做的教科書。這叫做奶奶的後備箱,一回合就可以找到一個物品在後備箱中,下一回合你說你在這個回合中找到了前一個物品和最新的物品......我想。在字符串的開頭找到元音和大寫char?
大部分這是一個使用函數的練習,我認爲我已經很好地完成了。但是我的輸出是完全錯誤的。我相信我已經找到了一個函數的問題,我應該分析第一個字符,並決定它是否需要AN或A或THE之前的字符串。我用來從小型數據庫中引入預定義項目的隨機函數可能存在問題。 int main()函數應該是相對完整的,這只是一個練習來掌握函數......我是這樣做的?寧可稱之爲新手體驗。
我以爲也許我遇到了getline bug,它會在空白處填上一個空行,並且從我的理解中,它是由cin.ignore()修復的;但是我輸入數據時所做的只是強迫我輸入兩次。哪個...我有點喜歡。也許我使用像isupper和.at()這樣的gizmos是錯誤的?我嘗試使用find_first_of,但它並沒有真正改變任何東西。
輸出調用存儲幹線和所有者奶奶,只是使用word1 word2 word3 ... wordn ....作爲項目發現留給我的輸出。
In grandma trunk you've found a
and an ord3 word1.
它完全混淆輸出。我開始認爲我給出的int main()體不完全是恆星。但是我不能對我的文章功能充滿信心。任何幫助都會令人難以置信。在許多書籍和朋友的建議中,我一直在努力使用它來教我一些關於編程的知識。這是一個非常頭痛的問題。
程序本身
#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;
}
TL; DR;明白一點,展示一個可重現的例子,你的具體問題是什麼。 –