0

So I am on Pset 2, the caesar problem. So far, function main checks to see if there is one command line argument, none, or two or more. I am trying to implement code so that an error is returned if the inputted argument isnt a digit. It was when i added this loop (to loop through the inputted argument and verify that is was all digits)

for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1]) == false)
            {
             printf("Usage: ./caesar key\n");
             return 1;
            }

that my code started giving the error "UndefinedBehaviorSanitizer:DEADLYSIGNAL ==1237==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7f12085a0924 (pc 0x000000422621 bp 0x7ffc711903b0 sp 0x7ffc711902e0 T1237) ==1237==The signal is caused by a READ memory access. #0 0x422620 (/root/sandbox/caesar+0x422620) #1 0x7f11260feb96 (/lib/x86_64-linux-gnu/libc.so.6+0x21b96) #2 0x402b29 (/root/sandbox/caesar+0x402b29)

UndefinedBehaviorSanitizer can not provide additional info. ==1237==ABORTING"

Here is my full code:

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        printf("Success\n");
        for (int i = 0; i < strlen(argv[1]); i++)
        {
            if (isdigit(argv[1]) == false)
            {
             printf("Usage: ./caesar key\n");
             return 1;
            }
        }
    }
    if (argc == 1 || argc >= 3)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}

Can someone please guide me in the right direction? I'm new to asking questions so please bear with me.

1
  • As an aside, this is a segmentation error. In the CS50 IDE it will tell you this, in the CS50 Lab it will tell you something like this with SEGV inside it. It means something is wrong with memory management, in this case because your data type is incompatible. Dec 4 '19 at 12:55
1

The "deadly signal" is kinda unique to our IDE. You get that because the IDE has encountered an error that it doesn't have a customized message for. So, let me fill in the blanks. if (isdigit(argv[1]) == false)

The isdigit() function takes a single char as input. But, argv[1] is a string. By attempting to shove a string down isdigit's throat, it chokes and spits out an error. You need to access one char at a time, ie., argv[1][i].

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

1
  • thank you! this worked
    – owwix
    Dec 4 '19 at 21:15

You must log in to answer this question.

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