Constexpr错误

时间:2015-10-15 22:22:01

标签: c++ templates

好的,我修复了错误。感谢你们。但是现在当我运行它时,我在菜单中选择D但是只有#34;你选择分割单词&删除段落中的重复项" &安培; "就是这样:"打印出来。之后它没有显示任何内容......任何人都知道它可能是什么?先感谢您!!

应该是这样的:

当选择第4个选项(“Split Words”)时,应将单词放入数组或结构中,并且每个单词应以循环显示。在执行此重复删除之后,程序必须确定重复的单词并消除它们。在此之后,应再次打印单词列表

#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 个答案:

答案 0 :(得分:1)

1)

set<s>::iterator 

应该是

set<string>::iterator

2)在局部变量的case语句周围添加括号。

case 'D':{

}
break;

case 'e':

case 'E':{


}
break;

答案 1 :(得分:0)

您正在使用空字符串创建stringstream,这反过来导致流为空。在实际读取字符串后,请考虑创建stringstream对象。 stringstream不保存对字符串对象的引用,因此对stringstream所基于的字符串的任何修改都不会反映流中的这些更改。