Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,212 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,262 people online right now. Registration is fast and FREE... Join Now!




isDigit() Assertion Error

 
Reply to this topicStart new topic

isDigit() Assertion Error

baconbeastnz
30 May, 2008 - 02:20 PM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 21

Hi there,

Basically im getting a runtime assertion error in my C code: ..\istype.c
expression (unsigned)(c+1) <= 256

If I take the isdigit() line out it works fine, but this is weird as I have the same piece of code elsewhere
in the file and it works fine.

Anyone have any idea what is going on here?

int m=0;
char theNO=0;
int Error_NotAllDigits=0;
char enterred[100];
int inputLength = strlen(enterred)-1;


for (m = 0; m < inputLength; ++m ) {
theNO = enterred[m];
if (isdigit(theNO)) {
}
}

This post has been edited by baconbeastnz: 30 May, 2008 - 02:21 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: IsDigit() Assertion Error
30 May, 2008 - 02:36 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,056



Thanked: 124 times
Dream Kudos: 1200
My Contributions
edit: I take that back, you are probably parsing the array for decimal numbers Let's see the whole code?

This post has been edited by KYA: 30 May, 2008 - 02:37 PM
User is online!Profile CardPM
+Quote Post

baconbeastnz
RE: IsDigit() Assertion Error
30 May, 2008 - 02:55 PM
Post #3

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 21

#include "stdafx.h"
#include <stdio.h>
#define STOP 0

void part2() {
int m=0;
char theNO=0;
int Error_NotAllDigits=0;
char enterred[100];
int inputLength = strlen(enterred)-1;



printf( "Enter a number then press enter:\n" );
fgets ( enterred, 100, stdin );

for (m = 0; m < inputLength; ++m ) {
theNO = enterred[m];

if (isdigit(theNO)) {

}

}
// ->20digits number
//calculate all prime n.os up to the square root of the integer
//compute prime factorization, (in increasing order) big n.o to small
//detects illegal input and keeps prompting if.

//get n.o, loop through check its all n.os
//forloop -> if stat(x1 and itself..)prime=1 //


printf( "Output: " );
}



int main()
{

//part1();
part2();


}

This post has been edited by baconbeastnz: 30 May, 2008 - 02:57 PM
User is offlineProfile CardPM
+Quote Post

baconbeastnz
RE: IsDigit() Assertion Error
31 May, 2008 - 02:46 AM
Post #4

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 21

Is anyone able to chuck run this code and let me know what happens ? Here is the complete code.. I can't figure out this damn assertion failure

#include "stdafx.h"
#include <stdio.h>
#include <SDL.h>
#define STOP 0



part1(void) {
char string[100];
int length = 0;
int i = 0, j = 0, k = 0, z=99;
char theCharacter;
int result[8] = {0,0,0,0,0,0,0,0}; // each element holds result of what prog checks
//0 =count, 1 =uc 2 =lc, 3 =digit, 4=vowel, 5 =cons, 6 =even, 7 =odd
char vowelList[7];
vowelList[0] = "a";
vowelList[1] = "e";
vowelList[2] = "i";
vowelList[3] = "o";
vowelList[4] = "u";
vowelList[5] = "w";
vowelList[6] = "y";

while (z > 0) {
printf( "\n\n" );
printf( "Enter a sentance then press enter \nInput: " );
fgets ( string, 100, stdin );
length = strlen(string)-1;
result[0] = length;

for (i = 0; i < length; ++i ) {
theCharacter = string[i];
printf( "%c : ", theCharacter);

if (isdigit(theCharacter)) {
result[3] = result[3]++;

if(theCharacter % 2 == 0) {
result[6] = result[6]++;
printf("even");
} else {
result[7] = result[7]++;
printf("odd");
}

} else {
for (k = 0; k < length-1; ++k ) {
if(theCharacter == vowelList[k]) {
result[4] = result[4]++;
} else {
result[5] = result[5]++;
}
}

if(isupper(theCharacter)) {
result[1] = result[1]++;
}
if(islower(theCharacter)) {
result[2] = result[2]++;
}

}


printf("\n");
} // end forloop
printf("\n\n .");

for (j = 0; j < 8; ++j ) {
printf("%d", result[j]);
}
printf("\n\n\n");
}
}

void part2() {
int m=0;
char theNO=0;
int Error_NotAllDigits=0;
char enterred[100];
int inputLength = strlen(enterred)-1;



printf( "Enter a number then press enter:\n" );
fgets ( enterred, 100, stdin );

for (m = 0; m < inputLength; ++m ) {
theNO = enterred[m];

if (isdigit(theNO)) {

}

}
// ->20digits number
//calculate all prime n.os up to the square root of the integer
//compute prime factorization, (in increasing order) big n.o to small
//detects illegal input and keeps prompting if.

//get n.o, loop through check its all n.os
//forloop -> if stat(x1 and itself..)prime=1 //


printf( "Output: " );
}



int main()
{

//part1();
part2();


}

User is offlineProfile CardPM
+Quote Post

mikeblas
RE: IsDigit() Assertion Error
31 May, 2008 - 05:49 AM
Post #5

D.I.C Head
**

Joined: 8 Feb, 2008
Posts: 155



Thanked: 1 times
My Contributions
We can't compile your code; it's got too many errors and you don't provide the complete source code. (We don't have your stdafx.h, and don't have your SDL.H file.)

The way you'll solve your problem is by doing a little debugging. The debugger can tell you what value you've passed to isdigit() when it throws this assertion, and that will lead you directly to the cause of your problem.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: IsDigit() Assertion Error
31 May, 2008 - 08:28 AM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
I know that you're calculating the string length incorrectly in the part2 function though, which is probably why the issue shows up only in that part... you're passing characters that are beyond the length of the input (not necessarily greater than 100, but beyond the user input).

char enterred[100];
int inputLength = strlen(enterred)-1;

That needs to be done after fgets... strlen retrieves the length of the string, which is the number of characters before the first null character (null terminated string).

Why is this significant? Well, there's a high probability that there are characters there that are greater than ASCII 127. If your compiler uses a signed char when you simply specify char, then ASCII 128 would be -128, 129 would be -127, etc.

The input parameter for isdigit is an int. Thus, the compiler will implicitly convert your char to an int. This requires a sign-extend to preserve a -128 value, or in binary...

As a char: 10000000
As an int: 11111111111111111111111110000000

Now, in the assert message that was displayed, there was a cast to unsigned. Casting that int to an unsigned yields:
4294967168

So, to fix this...
1.) Cast the character to an (unsigned char) before passing it to isdigit.
2.) Fix the strlen part I mentioned before.

This post has been edited by perfectly.insane: 31 May, 2008 - 08:28 AM
User is offlineProfile CardPM
+Quote Post

baconbeastnz
RE: IsDigit() Assertion Error
31 May, 2008 - 02:41 PM
Post #7

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 21

awesome!! yes you were right! By running

inputLength = strlen(enterred)-1; before fgets, the length was 100? or more than the actual input.

fgets ( enterred, 100, stdin ); ::: So fgets actually changes the length of the char[] !

Worked like a charm!
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: IsDigit() Assertion Error
31 May, 2008 - 03:44 PM
Post #8

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(baconbeastnz @ 31 May, 2008 - 03:41 PM) *

awesome!! yes you were right! By running

inputLength = strlen(enterred)-1; before fgets, the length was 100? or more than the actual input.

fgets ( enterred, 100, stdin ); ::: So fgets actually changes the length of the char[] !

Worked like a charm!


No.... the strlen function does not return the length of the array. It returns the length of the string inside the array. In C/C++, any variables that you declare must be initialized, and if you don't, they will contain garbage that appears to be random.

To answer your question... the length of inputLength would be unknown, as enterred is not initialized. It could be 20, 53, or even over 100 in some cases. It's all about whereever the first null character is in the array, if any. strlen is clueless as to how you declared enterred as there is no bounds checking in C.

So let's say that enterred contains a string "A string".

enterred[0] == 'A';
....
enterred[7] == 'g';
enterred[8] == 0;

(values at indicies 9..99 are irrelevant.. as long as [8] == 0).

That zero is important... that is what strlen looks for. So, strlen(enterred) in this case is 8. For an uninitialized array filled with garbage, the result is dependent on the garbage on the stack.

Try a printf on the inputLength in the program as it is now and you'll see what I mean.

The actual issue with isdigit is that you can't give it negative numbers... which if a signed char (same as a char on your compiler) is a number from -128 to 127 (which could be with random junk), then you'll get the assertion.

If you did a memset(enterred, 0, 100) before the strlen(...), then the strlen would return a zero for the string length.

This post has been edited by perfectly.insane: 31 May, 2008 - 03:46 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:17PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month