Help with Debug Assertion Failure

I am working on a project that converts a char array to a float, however I keep getting a debug assertion failure. I know it is in the if statement (code below), I just can't figure out why it's causing this.
_______________________________________________
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctype.h>
#include <math.h>
using namespace std;

const int LENGTH=24;
float MainStr;

void sremove(char strin[])
{
int counter=0;
char tempchar;
char tempstr[LENGTH];
for (int a=0; a<=LENGTH-1; a++)
{
tempchar=strin[a];
if(isdigit(tempchar))
{
tempstr[counter]=tempchar;
counter++;
}
}
MainStr=atof(tempstr);
//MainStr=atof(tempstr);
};


int main()
{
char InStr[LENGTH];
cout << "Enter some text: ";
cin.getline(InStr,LENGTH,'n');
cout << endl << "You entered: " <<InStr <<endl;
sremove(InStr);
cout <<"Converted to a float: "<< MainStr<<endl;
system("pause");
}
atof expects a null-terminated char array, tempstr will never have the '\0' character at the end
Please use [code][/code] tags
Last edited on
Thanks for that note, I'm new at this so syntax errors are no rare thing. I added '\0'to the end of tempstr( using tempstr[strlen(tempstr)]= '\0;). Still no change. The error goes away if I comment out the for loop though.

on a side note, it's interesting that you need to do 2 backslashes to post \0
Last edited on
strlen requires a null-tesminated string as well.
Since you have a loop ( and a counter! ) you have to keep track the actual length of the C-string yourself.
If you don't bother, you can use std::string instead of char arrays
Got it. Thanks for the help!
Topic archived. No new replies allowed.