2014-01-06 28 views
-2

這段代碼給了我很多奇怪的錯誤。無論出於何種原因,「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; 
    } 
+0

@Cory,難道我的回答幫你固定的問題? –

回答

1

您的newstring原型是錯誤的。

void newstring(string); 

應該

string newstring(string); 
+0

我認爲這是問題的一部分。它仍然無法正常工作。 –

+0

什麼不起作用..?我可以成功編譯它。你有任何新的錯誤或運行時問題? –

0

功能newstring被聲明爲void類型

void newstring(string); 

您可能無法創建類型的對象作廢,並在輸出流給他們

cout << "The pig Latin form of " << str << " is: " << newstring(str); 

同樣的功能沒有定義,因爲您所定義的名稱相同,但返回的std其他功能:: string的

string newstring(string sentence) 
                ^^^^^^^^^^^^^^^^^^ 
0
pigLatinString(currentword)+" "; 

pigLatinString返回一個字符串,但你沒有做這個結果什麼。

newstring返回newsentence,但是是空的。

也許你應該用pigLatinString返回的內容填充newsentence?

哦,現在我注意到你有兩個newstring ...這個空隙和一個字符串...