In the code snippet:

int Z;
boolean ParsedOk;
ParsedOk = sscanf( CommandLine, "%i", &Z ) == 1;

I find that a successful parse occurs for both valid numbers, but also for numbers that have trailing rubbish, such as '3Q', etc.

How can I detect (and reject) such malformed numerical input?

In the code snippet:

int Z;
boolean ParsedOk;
ParsedOk = sscanf( CommandLine, "%i", &Z ) == 1;

[...]
How can I detect (and reject) such malformed numerical input?

Check for trailing input as well.

if (sscanf(CommandLine, "%d%c", &Z, &ch) == 2 && (ch == '\n' || ch == '\0')) { 
    /* We got a winner */
} else {
    /* Anything else is uncivilized */
}

Edited 6 Years Ago by Aia: n/a

Hmm. thanks for that - kind of obvious, I guess! Now adapted to my actual needs and incorporated where required.

Hmm. thanks for that - kind of obvious, I guess! Now adapted to my actual needs and incorporated where required.

Yes, it needs adaptation. I left the part where it deals with spaces for you.

This question has already been answered. Start a new discussion instead.