0

Hello so I am doing PSET 2 Caesar and I tried to write a function to tell me if argv[1] is not a integer I want it to print the error message from the assignment. The problem is I'm very new with arrays and I think I wrote the function wrong because I keep getting a segmentation error and I'm not quite sure how to fix it. I'm including the entire program incase the error isn't where I think it is. Help?

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


bool isint(string argv[1]);
int main(int argc, string argv[])


{


// If there are no CLA or if more than 1 CLA print an error message and return from main a value of 1
    if (argc == 2)
        { printf("Success\n"); }
    if (argc != 2)
        { 
            printf("Usage ./caesar key\n"); 
            return 1; 
            
        }
// If CLA is not a decimal digit print Usage: ./caesarkey and return from main a value of 1
      if (isdigit(argv[1]) == 0)
        {
            printf("Usage ./caesar key\n");
        }
// Make sure k <= 26 Make k get correct alpha value

//Get string from user by asking for plaintext:

//print ciphertext

}


bool isint(string argv[1])
{
    int y = 0;
    for (int x = 0; (argv[1][x] != '\0'); x++)
        {
            if (isdigit(argv[1][x]) == 0)
                {
                    y++;
                }
                  
        }
    return y = 0;
}

1 Answer 1

0

Simple. isdigit and all of it's cousin is functions take a single char as input. But, argv[1] is a string, not a char. So, if you pass argv[1] as a parameter, it generates a seg fault.

You need to check each char in the string, one at a time.

Hints: for loop is your friend and so is argv[1][i].

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

3
  • I used that form and a for loop in the actual function when you scroll down and look at the actual function on the bottom. Commented Dec 28, 2020 at 23:39
  • Doesn't matter. The isint() function is never called by main(), so it's dead code. The seg fault comes from this: if (isdigit(argv[1]) == 0) in main. Even if isinit were called, it has a serious issue. You should never use reserved keywords or special reserved var names like argv as a function parameter. It causes problems.
    – Cliff B
    Commented Dec 28, 2020 at 23:43
  • oh i get it i made a typo i meant to use isint there not isdigit. so i did fix that but now im confused i just have to name it something else? Commented Dec 29, 2020 at 0:22

You must log in to answer this question.

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