944,175 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3546
  • C RSS
Apr 14th, 2005
0

my first recursion program... :/

Expand Post »
This is my first recursion project and i have no idea if i'm even going about it the right way...It's a palindrome checker, which checks for integers. I can't check it character by character as a string. It compiles clean but when i execute it i get weird results like "4280412". It is supposed to display "yes" or "no"

here is my code: could someone please help me figure out or tell me what i'm doing wrong?

  1.  
  2. void CLab13Dlg::OnClear() //button that clears both edit boxes
  3. {
  4. SetDlgItemText(IDC_NUMBER, "");
  5. SetDlgItemText(IDC_PALINDROME, "");
  6. }
  7.  
  8. void CLab13Dlg::OnCheck() //button to check if integer is a palindrome
  9. {
  10. GetDlgItemText(IDC_NUMBER, m_number);
  11. int length = m_number.GetLength(); //length of the string
  12. int number = atoi(m_number);
  13. palindrome(number, length);
  14. if(palindrome(number,length)==true)
  15. {
  16. m_palindrome.Format("%d", "Yes");
  17. SetDlgItemText(IDC_PALINDROME, m_palindrome);
  18. }
  19. else if(palindrome(number, length) == false)
  20. {
  21. m_palindrome.Format("%d", "No");
  22. SetDlgItemText(IDC_PALINDROME, m_palindrome);
  23. }
  24. }
  25.  
  26. bool CLab13Dlg::palindrome(int number, int length)
  27. {
  28. int value = (int)pow(10, length-1);
  29. if(length == 1)
  30. {
  31. return(true);
  32. }
  33. else if(number/value == number%10)
  34. {
  35. number = number%value;
  36. number = number / 10;
  37. length= length - 2;
  38. palindrome(number, length);
  39. return(true);
  40. }
  41. else
  42. {
  43. return(false);
  44. }
  45. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
blackdove is offline Offline
46 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: compiling error, tried debugging but no luck
Next Thread in C Forum Timeline: Can Someone please check my project & comment/tips





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC