Palindromes

This is a discussion on Palindromes within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by jewelz hmm..getting a compilation error..it's saying: error: expected identifier before '(' token indicating the line with while ...

  1. #46
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jewelz View Post
    hmm..getting a compilation error..it's saying:

    error: expected identifier before '(' token

    indicating the line with while (cin >> s)
    You need to surround the entire condition (both checks and the &&) inside one big set of parentheses.

  2. #47
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Ok now on to fix the problem that I have been having since completing the first part of the assignment (output just palindromes)...the compiler is telling me:

    pal.h:4: error: 'string' in namespace 'std' does not name a type
    pal.h:5: error: 'string' is not a member of 'std'
    pal.h:5: error: 's' was not declared in this scope

    and its saying that i cant use the isPalindrome function:

    error: 'isPalindrome' cannot be used as a function

    if u guys need to look at my code for header file and implementation file for two functions, it's on the previous pages..

  3. #48
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jewelz View Post
    Ok now on to fix the problem that I have been having since completing the first part of the assignment (output just palindromes)...the compiler is telling me:

    pal.h:4: error: 'string' in namespace 'std' does not name a type
    pal.h:5: error: 'string' is not a member of 'std'
    pal.h:5: error: 's' was not declared in this scope

    and its saying that i cant use the isPalindrome function:

    error: 'isPalindrome' cannot be used as a function

    if u guys need to look at my code for header file and implementation file for two functions, it's on the previous pages..
    That tells me you don't have #include <string> in your pal.h file (if that file needs string, you have to include <string> in that file).

  4. #49
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    yeah but one of my classmates said he didn't need to include that in his pal.h and it ran fine..plus i thought header files are generally not supposed to have them anyway?

  5. #50
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jewelz View Post
    yeah but one of my classmates said he didn't need to include that in his pal.h and it ran fine..plus i thought header files are generally not supposed to have them anyway?
    Then either (a) he is not using strings (surely he's not using char[] gasp horror) or (b) he's including some other file that in its turn includes string.h.

    Header files are most certainly supposed to include all the header files needed for its own content to make sense.

  6. #51
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    well i included the <string> and compiled it again, this time the only problem is that it's saying that there is an undefined reference to 'isPalindrome(std.....)

    i dunno what to make of that error.. pal.h is included but idk whats going on..

  7. #52
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And is isPalindrome prototyped in pal.h? More importantly, is it prototyped the way it is defined and the way it is called?

  8. #53
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    here is my code for the entire project:

    pal.h
    Code:
    #ifndef GUARD_PAL_H_
    #define GUARD_PAL_H_
    
    #include<string>
    
    std::string reverse(const std::string& s);
    bool isPalindrome(const std::string& s);
    
    #endif
    implementation file - pal.cc
    Code:
    #include "pal.h"
    #include <cstdlib> 
    
    //function declaration
    std::string reverse(const std::string& s)
    {
        string s2 = "";
        for (int i = 1; i < s.length(); i++)
    	{
    	    s2 = s2 + s[s.length() - i];
    	}
        s2 = s2 + s[0];
        return s2;
    }
    
    bool isPalindrome(const std::string& s)
    {
        return(s==reverse(s));
    
    }
    main function:
    Code:
    #include "pal.h"
    #include <string>
    #include <cstdlib>
    #include <iostream>
    
    using std::endl;
    using std::string;
    using std::cout;
    using std::cin;
    
    
    int main()
    
    {
        typedef string::size_type string_size;
        
        string s;
        string_size max = s.size();
        string longest = s;
        
        while((cin >> s) && (isPalindrome(s)== true)) {
    	string_size size = s.size();
    	if (size > max) {
    	    max = size;
    	    longest = s;
    	}
        }
    
        cout << "Longest Palindrome is " << "\"" << longest << "\""
    	 << "whose length is " << max << endl;
        
        return 0;
    }

  9. #54
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    there is never a reason to compare a bool to a bool.

    Code:
    isPalindrome(s)== true
    is identical to
    Code:
    isPalindrome(s)

  10. #55
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Oh, so linker error. Make sure pal.cc is in your project, or included in your command-line compilation.

  11. #56
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    Thanks guys i got it to compile, but i have a problem when running the program.. basically the program is supposed to read from the input and output the longest palindromes, along with it's length..however what it does is..it takes the first word inputted for example "abba" and then says that it is the longest palindrome right away and then waits for more input..i basically need the program to check through the list AFTER the user is done inputting words..also another problem is that the program exits if the user inputs a non-palindrome word.
    Last edited by jewelz; 02-22-2009 at 08:54 PM.

  12. #57
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    i believe either you changed your code from what's posted above or you have misspoken.

  13. #58
    Registered User
    Join Date
    Jan 2009
    Posts
    46
    yeah..i made a silly mistake =) anyway thanks for all the help guys..really appreciate it

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 03:31 PM
  2. palindromes and bool
    By justinc911 in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2003, 07:58 PM
  3. palindromes....
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 08-09-2003, 06:08 PM
  4. Palindromes
    By cman in forum C Programming
    Replies: 1
    Last Post: 05-05-2003, 07:39 AM
  5. strings and palindrome's
    By watshamacalit in forum C Programming
    Replies: 6
    Last Post: 01-07-2003, 06:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21