這段代碼給了我很多奇怪的錯誤。無論出於何種原因,「newstring」函數都沒有運行。我認爲它可能與它是cout語句的一部分有關,因爲如果我沒有記錯的話,如果我獨立於cout語句調用函數,它不會給出相同的錯誤。該程序需要字符串函數,但由於某種原因,新函數未運行。任何人都可以看看代碼?PigLatin C++函數
#include <iostream>
#include <string>
using namespace std;
void newstring(string);
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a sentence to be translated to Pig Latin: ";
getline(cin, str);
cout << endl;
cout << "The pig Latin form of " << str << " is: " << newstring(str);
system("PAUSE");
return 0;
}
bool isVowel(char ch)
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string :: size_type len;
bool foundVowel;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for (int counter = 1; counter < len - 1; counter++)
{
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
}
string newstring(string sentence)
{
string newsentence, currentword;
for (int i = 0; i < sentence.length(); i++)
{
if (sentence[i]==' ')
{
pigLatinString(currentword)+" ";
currentword.clear();
}
else
{
currentword+=sentence[i];
}
}
return newsentence;
}
@Cory,難道我的回答幫你固定的問題? –