Thread: errors may b concerning strings

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    errors may b concerning strings

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string letters = "Madam I'm Adam";
    	size_t length = letters.length();
    	bool isPalindrome;
    	unsigned index = 0;
    	string testStr;
    	
    	// Lowercase all the characters
    	// extract the letters without including punctuation and spaces
    	while (index < length)
    	{
    		if (isalpha(letters[index]))		
    			testStr += tolower(letters[index]);
    
    		index++;
    	}
    
    	// Now check if it is a palindrome
    	isPalindrome = true;
    	for (int i = 0; i < (testStr.length() / 2); i++)
    	{
    		if (testStr[i] != testStr[testStr.length() - (i-1)])
    		{
    			isPalindrome = false;
    			break;
    		}
    	}
    	cout << "\"" << letters << "\"" << isPalindrome ? "is" : "is not"
    		 << " a palindrome." << endl;
    
    	return EXIT_SUCCESS;
    
    }
    I can get it to compile due to some errors which I'm unable to understand, I'm compiling it with Visual Studio 2005

    The build log is

    EDIT:

    Got it. Was using C++/CLI so, had forgotten the parenthesis

    but still there are two errors

    Code:
    1>------ Build started: Project: C++ConsoleTest, Configuration: Debug Win32 ------
    1>Compiling...
    1>test.cpp
    1>c:\documents and settings\manzoor\my documents\visual studio 2005\projects\c++consoletest\c++consoletest\test.cpp(25) : warning C4018: '<' : signed/unsigned mismatch
    1>c:\documents and settings\manzoor\my documents\visual studio 2005\projects\c++consoletest\c++consoletest\test.cpp(34) : error C2296: '<<' : illegal, left operand has type 'const char [7]'
    1>c:\documents and settings\manzoor\my documents\visual studio 2005\projects\c++consoletest\c++consoletest\test.cpp(34) : error C2297: '<<' : illegal, right operand has type 'const char [15]'
    1>Build log was saved at "file://c:\Documents and Settings\manzoor\My Documents\Visual Studio 2005\Projects\C++ConsoleTest\C++ConsoleTest\Debug\BuildLog.htm"
    1>C++ConsoleTest - 2 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Well, it would be great if told how to resolve the warning
    Last edited by manzoor; 09-09-2008 at 05:35 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to put parenthesis around your ternary operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string letters = "Madam I'm Adam";
    	size_t length = letters.length();
    	bool isPalindrome;
    	unsigned index = 0;
    	string testStr;
    	
    	// Lowercase all the characters
    	// extract the letters without including punctuation and spaces
    	while (index < length)
    	{
    		if (isalpha(letters[index]))		
    			testStr += tolower(letters[index]);
    
    		index++;
    	}
    
    	
    
    	// Now check if it is a palindrome
    	isPalindrome = true;
    	for (int i = 0; i < (testStr.length() / 2); i++)
    	{
    		if (testStr[i] != testStr[testStr.length() - (i-1)])
    		{
    			isPalindrome = false;
    			break;
    		}
    	}
    	cout << "\"" << letters << "\" " << (isPalindrome ? "is" : "is not")
    		 << " a palindrome." << endl;
    
    	return EXIT_SUCCESS;
    
    }
    it compiled successfully but the it isn't producing correct results

    could you spot the mistake

    what I'm entering is "Madam I'm Adam."
    which would be evaluated to "madamimadam"

    in which case it is a palindrome but the program showing it isnt

    IN code::blocks im able to compile it and also in visual studio

    but in visual studio when I run i get a string subscript out of range error during the run time? whats the error

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The warning can be resolved by making i in your for loop an unsigned int, as that's what is returned by string.length().

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    it compiled successfully but the it isn't producing correct results

    could you spot the mistake

    what I'm entering is "Madam I'm Adam."
    which would be evaluated to "madamimadam"

    in which case it is a palindrome but the program showing it isnt

    IN code::blocks im able to compile it and also in visual studio

    but in visual studio when I run i get a string subscript out of range error during the run time? whats the error

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The Visual studio error is a key, isn't it. Why don't you add some output to show you what you are actually comparing?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How good is your math?
    Code:
    testStr.length() - (i-1)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Very bad,


    what i wanted was testStr.length() - i - 1;


    thanks anon
    Last edited by manzoor; 09-09-2008 at 06:35 AM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    umm... that's the same thing.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    umm... that's the same thing.
    No, since testStr.length() - (i-1) is equal to testStr.length() - i + 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    No, since testStr.length() - (i-1) is equal to testStr.length() - i + 1.
    And when i is zero, that's length()+1, which is TWO positions beyond the end of the string.

    I actually fixed the problem using length() - (i+1), which is the same as the length() - i - 1 expression. I prefer my variant, as it's clearer to subtract i+1 than it is to subtract i and then subtract 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by laserlight View Post
    No, since testStr.length() - (i-1) is equal to testStr.length() - i + 1.
    Obviously I'm missing something. How are those equal?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Obviously I'm missing something. How are those equal?
    If you massage the math:
    -(i-1) -> -i + 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by robwhit View Post
    Obviously I'm missing something. How are those equal?
    Suppose i equals 5, and suppose the first part is 10. So: 10 - (5-1) = 10 - 4 = 6, while 10 - 5 - 1 = 5 - 1 = 4, and 10 - 5 + 1 = 5 + 1 = 6.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    OIC. thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Header File Errors...
    By Junior89 in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2007, 12:28 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM