Search code examples
cstringintstructureisalpha

Checking if int is not alpha


I have a structure that has

int Hour
int Min
int Sec

I'm reading string from file. File has text like: 23:21:30 12:32:54

I split whole line to separate times and then I do second split to structure. I'm doing

structure[x].Hour = atoi(token);

Then I need to check if structure[x].Hour is not alpha.

if(isalpha(structure[x].Hour)){//DO Something} 

is not working. Any ideas?


Solution

  • You don't really ask a question or tell us what problem you have, but I'll help you anyway...

    What you are doing is in the wrong order. If you call atoi with a non-numeric strings it will return 0. You then check to see if the integer 0 is not a letter, which it of course isn't. You need to do it in the opposite order: First check that you don't have a non-numeric string (which you have to do in a loop); And then convert it to an integer.

    This can actually be done in a single standard library function call, but not to aoti which doesn't have that support, but with the strtol function.

    The strtol function have error checking built in, and support to help you figure out if the whole string was converted or not.