Infinite loop when checking character array length?
Program is to check the users input for social security number. Need to make sure it is all digits, no letters, and need to be nine digits long. My problem is when checking the length of the ssn it turns into an infinite loop. Is there a different way to check the user input?
WHERE ITS CALLED IN PROGRAM:
if (check_social_input(ssn, SIZE))
{
cout << "...Valid" << endl;
break;
}
else
{
cout << "That is not the proper format of the Social Security Number. " << endl;
cout << "\nHere is an example: ";
cout << " 123456789";
}
}while (!check_social_input (ssn,SIZE));
CHECKING LENGTH, ALPHA AND DIGIT:
bool check_social_input(char id_num[], int size )
{
int count; // loop counter
//Test to make sure the ssn has been inputed in correct length (i.e. 9 digits long)
for (count = 0; count < size - 1; count ++)
{
if (!id_num[size])
return false;
}
//Test to make Test to make sure ssn has been inputed in the correct form WITHOUT ANY LETTERS.
for (count = 0; count < size -1 ; count++)
{
if (isalpha(id_num[count]))
return false;
}
//Test to make sure ssn has been inputed in the correct form with numbers only.
for (count = 0; count < size - 1 ; count++)
{
if (!isdigit(id_num[count]))
return false;
}
return true;
}
[1517 byte] By [
sunshine1] at [2007-12-5 11:58:04]

# 1 Re: Infinite loop when checking character array length?
For checking length of a string use the strlen() function.
To check if the string is numeric or not, try these:
http://www.codeguru.com/forum/showthread.php?t=266112
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
iPrank at 2007-12-5 14:47:14 >

# 2 Re: Infinite loop when checking character array length?
I am actually a beginner and in my class we have to stay around what is shown in my code, it there any way to keep the code i have and check to make sure the user input is correct, everything else checks out its just the length.
# 3 Re: Infinite loop when checking character array length?
I don't understand what if (!id_num[size]) does. It is not counting the string length.
Inside that loop, you should find the position of null string (\0) in the array. Then the 'count' variable will have length of the string.
Also, you don't need both Abhpabet and Digit checks. Just check if the character is digit or not. Return false if it is not a digit.
iPrank at 2007-12-5 14:49:17 >

# 4 Re: Infinite loop when checking character array length?
ok, but how would i find the position on \0, is it by using .length?
# 5 Re: Infinite loop when checking character array length?
Still stuck on checking hate length of a correctors array...does anyone have so insight?
# 6 Re: Infinite loop when checking character array length?
id_num[size] should be '\0'
# 7 Re: Infinite loop when checking character array length?
Still pretty new can some one expand on: id_num[size] should be '\0'
# 8 Re: Infinite loop when checking character array length?
Is the content of the array static? If so you really don't want to be using StrLen and such to check each and every time.
#define NUM_KEYWORDS (7)
const unsigned int keywordlens[] = {2, 3, 5, 5, 4, 8, 6};
const char *keywords[] = {
"if",
"for",
"while",
"var32",
"void",
"function",
"return"
};
Above is an example i done a while back in C++ (keyword coloring) i stored the length and the number like so. Its just faster then checking using StrLen if its going to be static.
Paul M at 2007-12-5 14:54:15 >

# 9 Re: Infinite loop when checking character array length?
if (!id_num[size])
return false;
...is equivalent to...
if (id_num[size] == '\0')
return false;
because '\0' evaluates to zero (and therefore false).
# 10 Re: Infinite loop when checking character array length?
The thing about this project i am making is it needs to check the user input wich in this case it is going to be the ssn, (9 digits long, all number, and if it is longer or has letters in it it need to loop and ask for ssn again in the correct form)...I am stuck just at this i can get the user input only if 9 if it is less or has letters it will loop and say it was the wrong input but when putting in more it wont loop or it will say it is vaild. How can i check that the length is correct and anything longer will get destroyed in the buffer.
bool check_social_input(char id_num[], int size )
{
int count; // loop counter
//Test to make sure the ssn has been inputed in correct length (i.e. 9 digits long)
for (count = 0; count < 10; count ++)
{
if (id_num[9] == '\n')
return false;
}
//Test to make sure ssn has been inputed in the correct form with numbers only.
for (count = 0; count < 10; count++)
{
if (!isdigit(id_num[count]))
return false;
}
return true;
}
# 11 Re: Infinite loop when checking character array length?
Why are you passing the string AND the size into the function?
# 12 Re: Infinite loop when checking character array length?
Your correct wossname...I resolved the problem; however, will this cause overflow even though there are no errors?
VARIABLES:
char exit_str[5]; // null terminated string for yes/no input
char ch;
const char SIZE = 50; // charector array buffer
ERROR CHECKING:
bool check_social_input(char id_num[])
{
int count; // loop counter
//Test to make sure ssn has been inputed in the correct form with numbers only.
for (count = 0; count <9; count++)
{
if (!isdigit(id_num[count]))
return false;
}
//Test to check length of ssn (cannot be over 9 digits long)
if (id_num[9] != '\0')
{
return false;
}
return true;
}