Error with lambda

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â



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117


#include<iostream> 
#include <cmath>
#include <algorithm>


using namespace 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)
	
		return true;
	//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]))
	
		return false;
	
	
	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;
}
Last edited on
if your compiler doesn't understand lambda then write a regular function instead.
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
Last edited on
As stated before use a regular function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
bool alpha(char c)
{
     return !isalpha(c);
}

int main()
{
    std::string temp = "He4ll3o";

    temp.erase(std::remove_if(temp.begin(), temp.end(), alpha), temp.end());

    for(unsigned i = 0; i  < temp.size(); ++i)
        std::cout << temp.at(i);


    return 0;
}
Last edited on
Topic archived. No new replies allowed.