I'm having difficulties in validation step in Caesar. My logic here is to pass through each digit and check if it is a number character and if not, abort. If they all are, it will convert each iteration of character to a numerical integer and output it.

int main(int argc, string argv[])
    string s = argv[1];

    for (int i = 0, n = strlen(s); i < n; i++)
        if (isdigit('s[i]') != true)
            printf("Usage: %s key\n", argv[0]);
            return 1;
            int converted = atoi("s[i]");
            printf("Success\n%i\n", converted);

When I run this, it returns

> UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1005==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7f66bda49f7a (pc 0x00000042813d bp 0x7ffc6be7fe40 sp 0x7ffc6be7fdb0 T1005)
==1005==The signal is caused by a READ memory access.
    #0 0x42813c in main /root/sandbox/caesar1.c:13:13
    #1 0x7f65d6d5fb96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
    #2 0x402b39 in _start (/root/sandbox/caesar1+0x402b39)

UndefinedBehaviorSanitizer can not provide additional info.

I've never seen this type of error. What's wrong with my code?

Thank you!


Your usage of isdigit() and atoi() are incorrect. In each case, you've enclosed the variable name in single or double quotes.

When enclosed in double quotes, the text is taken as a literal string. In other words, "s[i]" treats the var name as just a plain word like "cat" and feeds the literal string of characters to the atoi() function.

Single quotes means that you're trying to process a single character. That's working similarly, except that multiple characters are enclosed in single quotes, which really messed things up! ;-)

Remove all of the quotes and try again. You may have other problems, but that should cure this one.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .