-4

Hello Everyone: I'm currently working on a code that checks if a string is a palindrome using recursion. I've been trying to fix it for a few hours now and I have gotten less errors, thank god. But I'm stuck on the part of my code where the error is:

ERROR!
g++ /tmp/w8b7ylecAL.cpp
/tmp/w8b7ylecAL.cpp:4:20: error: expected ')' before '(' token
    4 | struct palin(string (text, int left, int right)){
      |             ~      ^~
      |                    )
/tmp/w8b7ylecAL.cpp: In function 'int main()':
/tmp/w8b7ylecAL.cpp:48:47: error: expected primary-expression before '<<' token
   48 |     cout << "this is a palindrome: " << palin << endl;
      |                                               ^~
/tmp/w8b7ylecAL.cpp:50:51: error: expected primary-expression before '<<' token
   50 |     cout << "this is not a palindrome: " << palin << endl;
      |                                                   ^~
 

I think maybe it has to do with "palin" which is a structure above my main function. Can I pass a struct a function in my main or is it a syntax issue maybe?

Here is my code for context:

    #include <iostream>
    
    using namespace std;
     
    struct palin(string (text, int left, int right)){
        int text = string; 
        int input1(stirng input1)
        {
            if (input1 != text.length()/2)
            {
                return false; 
            }
            else{
                return true; 
            }
        }
    
        int input2(string input2)
        {
            if (input2 != text.length()/2)
            {
                return false; 
            }
            else{
                return true; 
            }
        }
    
        int input3(string input3)
        {
            if (input3 != text.length()/2)
            {
                return false; 
            }
            else{
                return true; 
            }
        }
    
    };
    
    int main(){
    
    //declaring the variables input1, input2, input3
        string input1 = "Kayak"; //will return true
        string input2 = "Mr. Owl ate my metal worm"; //will return true
        string input3 = "Hello";//return false 
    
        cout << "true " << palin << endl; 
    
        cout << "false " << palin << endl; 
    
    }

I expected my code to return true if it was a palindrome and false if not. I know true and false is usually used in bool, However I have not learned bool yet so I do not know how to apply it in my code, especially since I'm very much new to c++

nenamuse
  • 1
  • 1
  • 3
    What are you trying to do with this line? `struct palin(string (text, int left, int right)){`? – Yksisarvinen Sep 02 '23 at 20:16
  • `palin(string (text, int left, int right)){` <-- Your parens are messed-up. Fix that first. – Dai Sep 02 '23 at 20:17
  • @Dai The whole line is messed up, that's a struct declaration right there – Yksisarvinen Sep 02 '23 at 20:17
  • 3
    It's more than just that line. There's nested function definitions, code inside that makes no sense, and the main function doesn't make sense either. This isn't even close to working code. Something is fishy about this question. And why is the file being compiled a randomly-named file in /tmp? – Sebastian Redl Sep 02 '23 at 20:24
  • I'm trying to set up my constructors but now that I look at it they're not in my struct. – nenamuse Sep 02 '23 at 20:27
  • @SebastianRedl Hey I see what you mean, I'm using an online compiler. https://www.programiz.com/. could that be the reason why /tmp is showing up? – nenamuse Sep 02 '23 at 20:30
  • @nenamuse Why aren't you using a local build environment? Online compilers are not appropriate for anything beyond trivial examples. – Dai Sep 02 '23 at 20:30
  • It looks like you're making up syntax as you go to try and get rid of errors. C++ really doesn't lend itself to that kind of workflow. I suggest you read up on the basics of defining structs and their member functions in [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/) – alter_igel Sep 02 '23 at 20:36
  • @Dai I do, I use VS code, However when I tried to copy the error message it wouldn't let me copy it, sorry. – nenamuse Sep 02 '23 at 20:38
  • 4
    I’m voting to close this question because it contains too many different syntactic errors to address in a single answer. – alter_igel Sep 02 '23 at 20:39
  • `"However I have not learned bool yet"` - sorry but you really need to go back to square one. I suggest learning a different language while you're at it. C++ is enormously complex. Have you tried Python? – alter_igel Sep 02 '23 at 20:42
  • @alter_igel Oh my god!!!! I'm dying. You're telling me my teacher is going about teaching us cpp the wrong way, We have gone through structures but very briefly.... FML. – nenamuse Sep 02 '23 at 20:51
  • Yes, and you won't be the first one on Stackoverflow to post evidence of an incompetent C++ instructor. A competent C++ instructor would've explained how to check if a `std::string` contains a palindrome using just one line of code. – Sam Varshavchik Sep 02 '23 at 22:05
  • To get debugging help, you should reduce your code to a [mre] where the error you are asking about is the **first** error reported by the compiler. *(Don't ask about how to use `palin` before you have a valid definition for it. The definition can be incomplete from a final-product viewpoint, but it should be valid. Yours is not.)* – JaMiT Sep 02 '23 at 22:43

0 Answers0