When I run my program on cpp.sh it runs great, but when I run it on secure shell, I am getting errors and it has to do with my lambda function. I am guessing it has to do with the compiler being outdated, but it has to run on that one according to my professor. Anyone have another way to remove special characters? I commented where the error is. Line 98 and 99
The errors are:
error: expected primary-expression before â[â token
error: expected primary-expression before â]â token
error: expected primary-expression before âcharâ
#include<iostream>
#include <cmath>
#include <algorithm>
usingnamespace std;
//Create a structure called Sentence
struct Sentence
{
int CountVowels(string , int);
public:
Sentence (string);
bool isPal(string , int);
void Print();
string s;
int numVowel;
int length;
//~Sentence();
};
Sentence :: Sentence (string b)
{
s = b;
length = 0;
numVowel = CountVowels(s, 0);
}
//Count Vowels function using recursion
int Sentence :: CountVowels(string myWord, int startindex)
{
length ++;
int pandi;
if(myWord[startindex])
{
if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
{
pandi = 0;
}
else pandi = 1;
return pandi + CountVowels(myWord, startindex + 1);
}
return 0;
}
// Check if it palindorme using recursion
bool Sentence :: isPal(string myWord, int size)
{
int r = myWord.size() - size;
int t = size - 1;
//size = r will be true whenn the size of the string is even and the 2 middle characters have been checked
if (size == r || r == t)
returntrue;
//r = t will be true when the size of the string is odd and the two characters on either side of the middle character have been checked
if (tolower(myWord[r]) != tolower(myWord[t]))
returnfalse;
return isPal(myWord, -- size);
}
//Display the sentence
void Sentence :: Print()
{
cout << s [-- length];
if (length == 0)
{
cout << "" << endl;
return;
}
Print ();
}
//Main function
int main ()
{
//Holds user sentence
string userW;
//Ask user to enter a sentence
cout << "Enter a sentence: \n";
getline(cin, userW);
//Removes special characters
//THIS IS WHERE ERROR IS
userW.erase(remove_if(userW.begin(), userW.end(), [](char c)
{return !isalpha(c); }), userW.end());
//Creates userSent under Sentence
Sentence userSent(userW);
//Display the number of vowels
cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
cout << "" << endl;
//Display if the sentence is a palindrome or not
cout << "The sentence" << " is" <<
(userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " not Palindrome\n");
cout << "" << endl;
//Display the sentence backwards
cout << "The sentence spelled backwards is: " << endl;
userSent.Print();
return 0;
}
Are you sure you're compiling with a compiler that is compliant with lambda functions? Lambda was introduced in C++11, so make sure your compiler is C++11-compliant. For some IDEs, like Code::Blocks, you may need to tell it to compile for C++11 in the project settings, as some will default to an older standard.
If you cannot get it to work with Lambda, just write it using a regular function.
I use cpp.sh to check it online and it works fine, but my university uses secure shell to turn in our assignments and it is not wanting to compile there. I also replaced that line with userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());
and this time it didn't want to work on cpp.sh
it said error: no matching function for call to copy_if