2015-10-15 73 views
-2

好的我修復了錯誤。感謝你們。但現在當我運行它時,我在菜單中選擇了D,但只有「您選擇拆分單詞&刪除段落中的重複項」&「這就是:」打印出來。它沒有顯示任何東西之後......任何人都知道它可能是什麼?先謝謝你!!Constexpr錯誤

這是應該的:

當選擇第四選擇(「分割詞」),詞語應放入陣列或您的結構和每個單詞應該以循環顯示。在重複刪除之後,程序必須確定重複的單詞並將其刪除。在此之後,單詞列表應再次

#include <iostream> 
#include <cmath> 
#include <cstdlib> 
#include <string> 
#include <algorithm> 
#include <cctype> 
#include <sstream> 
#include <set> 

using namespace std; 
int main() 
{ 

    string s; 
    char selection; 
    string w; 
    string buf; 


    cout << "Enter a paragraph or a sentence : " ; 

    getline(cin, s); 

    int sizeOfString = s.length(); 

    //cout << "The paragraph has " << sizeOfString << " characters. " << endl; ***Dummy call to see if size works. 

    //cout << "You entered " << s << endl; *** Dummy function !! 

    cout << "" << endl; 

    cout << "     Menu   " << endl; 
    cout <<"  ------------------------" << endl; 
    cout << "" << endl; 
    cout << "A -- Convert paragraph to all caps " << endl; 
    cout << "B -- Convert paragraph to all lowercase " << endl; 
    cout << "C -- Delete whitespaces " << endl; 
    cout << "D -- Split words & remove duplicates " << endl; 
    cout << "E -- Search a certain word " << endl; 
    cout << "" << endl; 
    cout << "Please select one of the above: " ; 
    cin >> selection; 
    cout << "" << endl; 

    stringstream ss(s); 
    set<string> tokens; 

    switch (selection) //Switch statement 
    { 
     case 'a': 
     case 'A': cout << "You chose to convert the paragraph to all uppercase" << endl; 
        cout << "" << endl; 
        for(int i=0; s[i]!='\0'; i++) 
        { 
         s[i]=toupper(s[i]); 
        } 
        cout << "This is it: " << s << endl; 
        break; 
     case 'b': 
     case 'B': cout << "You chose to convert the paragragh to all lowercase" << endl; 
        cout << "" << endl; 
        for (int i=0; s[i] !='\0'; i++) 
        { 
         s[i]=tolower(s[i]); 
        } 
        cout << "This is it: " << s << endl; 
        break; 
     case 'c': 
     case 'C': cout << "You chose to delete the whitespaces in the paragraph" << endl; 
        cout << "" << endl; 
        for(int i=0; i<s.length(); i++) 
        if(s[i] == ' ') s.erase(i,1); 
        cout <<"This is it: " << s << endl; 
        break; 
     case 'd': 
     case 'D': cout << "You chose to split the words & remove the duplicates in the paragraph" << endl; 
        cout << "" << endl; 

       // Insert the string into a stream 

        // Create vector to hold our words 

        while (ss >> buf) 
        tokens.insert(buf); 

        cout << "This is it: " << endl; 

        for (set<string>::iterator it = tokens.begin(); it != tokens.end(); ++it) 
        { 
         cout << *it << " "; 
        } 

     cout << endl; 

     break; 




     case 'e': 
     case 'E': cout << "You chose to search for a certain word in the paragraph. " << endl; 
        cout << "" << endl; 
        cout << "Enter the word you want to search for: "; 
        cin >> w; 

        s.find(w); 
        if (s.find(w) != std::string::npos) 
        { 
         cout << w << " was found in the paragraph. " << endl; 

        } 
       else 
        { 
        cout << w << " was not found in the paragraph. " << endl; 
        } 




    } 


    return 0; 
} 
+2

如果是D,在你的for循環中,你有'set '。 's'不是一種類型。你打算寫我想的字符串。 –

+0

歡迎來到Stack Overflow!添加一個更具描述性的標題將更好地關注這篇文章。另外,我會盡量減少你發佈的代碼作爲例子。 – brandaemon

+1

'set :: iterator' should' set :: iterator' – NathanOliver

回答

1

1)印刷

set<s>::iterator 

應該

set<string>::iterator 

2)添加周圍的局部變量的情況下語句括號。

case 'D':{ 

} 
break; 

case 'e': 

case 'E':{ 


} 
break; 
+0

我不喜歡這些括號......他們看起來很傷心。 – MicroVirus

+0

在一種情況下聲明的局部變量在下面仍然可見。但是初始化會被切換到下面的情況。 – QuentinUK

0

您正在使用空字符串創建stringstream,這反過來導致流爲空。考慮在實際讀取字符串後創建stringstream對象。 stringstream不包含對你的字符串對象的引用,因此對stringstream所基於的字符串的任何修改都不會反映這些流中的更改。