4 Replies - 1987 Views - Last Post: 20 May 2011 - 10:24 PM Rate Topic: -----

#1 theant4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Debug assertion failure... what does this mean?

Posted 20 May 2011 - 11:11 AM

Basically this program consists of three simple functions -- one to return true of a cstring entered by the user is a palindrome, one to return true of the cstring consists of all alphabetic characters, and one that simply counts and returns the number of times a specific character occurs within the string. I got rid of all the errors so I thought it would work, but I'm getting a debug error. When the command prompt appears I enter my cstring ("racecar" for the sake of the palindrome part) and as soon as I enter it the error comes up: "Debug Assertion Failed!" and then says "Expression: (unsigned)(c+1) <= 256" and I have no clue what this means. What do the c and the 256 even have to do with my program? Can someone help me out?

Here's the program:

#include <iostream>
#include <cctype>
#include <cstring>

using std::cout;
using std::cin;
using std::endl;

bool IsPalindrome( char string[] );
bool IsAlphaStr( char string[] );
int CountChar( char string[], char letter );

int main()
{
	char string[20];
	char letter;
	int count;
	bool palindrome, alpha;

	cout << "Please enter your word of choice: " ;
	cin >> string;

	palindrome = IsPalindrome( string );
	alpha = IsAlphaStr( string );
	
	count = CountChar( string, letter );
	cout << "The letter you chose occurs " << count << " times in the string." << endl;

return 0;
}

/***********************************************
Name: IsPalindrome
Parameters: string
This function returns true if the string is a
palindrome and false if it is not
***********************************************/
bool IsPalindrome( char string[] )
{
	char temp[20];
	bool palindrome = false;

	strcpy( temp, string );
	strrev( temp );

	if ( stricmp ( string, temp ) == 0 )
		palindrome = true;

return palindrome;
}


/***************************************************
Name: IsAlphaStr
Parameters: string
This function returns true if the string contains
all alphabetic characters and false if it does not
****************************************************/
bool IsAlphaStr( char string[] )
{
	bool alpha = false;
	if ( isalpha ( string[20] ) == 1 )
		alpha = true;
	else 
		cout << "Error: contains non-alpha character!" << endl;

return alpha;
}


/*************************************************
Name: CountChar
Paramters: string, specified character
This function counts and returns the number of
times a specified character occurs in the string
*************************************************/
int CountChar( char string[], char letter )
{
	cout << "Enter the letter you wish to count: " ;
	cin >> letter ;

	int count = 0;

	for ( int i = 0; i < 20; i++ )
	{
		if ( i == letter )
			count + 1;
	}

return count;
}


Edit: Oops, I think I posted this 3 times -- my computer was doing something weird! :/

This post has been edited by theant4: 20 May 2011 - 11:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Debug assertion failure... what does this mean?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Debug assertion failure... what does this mean?

Posted 20 May 2011 - 11:20 AM

Maybe you have a conditional breakpoint on somewhere that asserts when that condition is true. :)
Was This Post Helpful? 0
  • +
  • -

#3 theant4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Re: Debug assertion failure... what does this mean?

Posted 20 May 2011 - 11:25 AM

Meaning what, exactly? Sorry I'm a first-year programmer and I still have a lot of learning to do...

Edit: Some additional info -- I took another look at the error pop-up and it's saying it's on line 56... which, in Visual Studio, is the first line of the comment documenting the IsAlphaStr function...

This post has been edited by theant4: 20 May 2011 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Debug assertion failure... what does this mean?

Posted 20 May 2011 - 11:44 AM

It's likely a problem between your code and the standard headers.

There are many issues with your code, beginning with the problem being in your IsAlphaStr() function just by debugging. And why are you using character pointers instead of string objects?
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Debug assertion failure... what does this mean?

Posted 20 May 2011 - 10:24 PM

You run the code in the debugger.

You wait for the assert to happen, and then it will be trapped in the debugger (rather than some OK/Cancel dialog).

(I can't remember where), you can display a stack trace of how your code ended up calling the function with the assert. Find the most recent function call in YOUR code (not OS functions, or library functions), and see what you did.

My guess is you're passing garbage characters with your random array subscript at this line.
> if ( isalpha ( string[20] ) == 1 )
This does NOT run isalpha over 20 characters of your string.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1