0

My code compiles but when I run it I get the error: Segmentation fault (core dumped). I looked at similar questions to this, but I think I'm using isalpha correctly. Any insight will be appreciated. (As an aside, I find my code highly unreadable. If anyone has any tips for me regarding readability and simplicity that would be great!) Here's my code:

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


int cipher(int x);
string k;
string s;

int main(int argc, string argv[]){

    if (argc != 2){
        printf("I can't do that.  argc must be 2.\n");
        return 1;
    }

    string k = argv[1];

    for (int i = 0, n = strlen(k); i < n; i++){
        if (!isalpha(k[i])){
            printf("Vigenere key has non-alphabetical characters.\n");
            return 1;
        }

    }

    string s = GetString();

    for (int i = 0, n = strlen(s); i < n; i++){
        printf("%c", cipher(s[i]));
    }

    printf("\n");

}


int pos = 0;

int cipherlow(int s);
int cipherupp(int s);


int cipher(int s){
    int j = strlen(k);

    if(isalpha(s)){
        //get pos in keyword
        pos = pos + 1;
        pos = pos % j;

        if(islower(s)){
        //print: pos in keyword * shift + s[i]
            printf("%c", cipherlow(s));
        }
        else{
            printf("%c", cipherupp(s));
        }
    }
    else{
        //just print s[i]
        printf("%c", s);
    }


    return 0;
}

int cipherlow(int s){
    int p = k[pos] + s - 97;
    p = p % 26 + 97;
    return p;
}

int cipherupp(int s){
    int p = k[pos] + s - 65;
    p = p % 26 + 65;
    return p;
}  
1

This is what happens when you redeclare a variable, and a good example of why global variables can cause problems.

Here's what's happening. You first declare string k; as a global var outside of main and all of the other functions. Next, you declare it again inside of main with string k = argv[1];. Later, you call cipher() and the first line inside of cipher is int j = strlen(k);. Unfortunately, since you are no longer inside of main(), the k that points to argv[1] is out of scope and cannot be accessed here. Instead, cipher() is using the global k, which is currently = null. So, when you try to get strlen(k), you get a seg fault.

There is no need to use global variables in this pset. You should try to recode your work to eliminate them.

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

3
  • Cliff might I just say you are an awesome person. – Michael Sanchez Aug 18 '15 at 0:00
  • You can say that as much as you'd like. ;-) BTW, your code is quite readable. It needs one or two minor tweaks, but I've seen a lot worse. Try running your source file through style50 to get suggestions on how to improve. – Cliff B Aug 18 '15 at 0:27
  • I didn't even know there was a style50...thanks. – Michael Sanchez Aug 18 '15 at 0:46

You must log in to answer this question.

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