-2

I am writing a program that encrypts plain text to ciphertext. I am receiving a Segmentation Fault, core dumped error when I go to run my program.

Here is my code:

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

int main(int argc, string argv[])
{
    int k = 0;

    // continues the program if 2 and no more than 2 command line arguments exist and argv[1] contains alphabetical characters only
    if(argc == 2 && argc == isalpha(argv[1]))
    {
        k = atoi(argv[1]);
    }

    // re-prompts user to enter only 2 command line arguments and the second should consist of only alphabetical characters
    else
    {
        printf("You must enter a command line argument using only alphabetical characters!\n");
        return 1;
    }

    string text = GetString();
    int cipher;
    int key;

    // loops through each charcter in key and gives each character a valule to add to plain text
    for (int j = 0, n = strlen(argv[1]); j < n; j++)
    {
        if (isalpha(argv[1][j]))
        {
            if (isupper(argv[1][j]))
            {
                key = 26 - (91 - argv[1][j]);
            }

            else
            {
                key = 26 - (123 - argv[1][j]);
            }
        }

        else
        {
            key = argv[1][j];
        }

        // loops through plaintext entered by user and changes text to ciphertext according to key
        for (int i = 0, l = strlen(text); i < l; i++)
        {
            if (isalpha(text[i]))
            {
                cipher = (text[i] + key) % n;
                printf("%c", cipher);
            }

            else
            {
                printf("%c", text[i]);
            }
        }
    }
    printf("\n");

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
howdydoody
  • 93
  • 3
  • 17
  • 1
    I am just curious: Did this code segment actually build without errors with the main being declared as `int main(int argc, string argv[])` ? (as haccks has said, there is no ***string*** type in C) – ryyker Feb 11 '14 at 16:17
  • 1
    `if(argc == 2 && argc == isalpha(argv[1]))` Meaning unknown – BLUEPIXY Feb 11 '14 at 16:18
  • 1
    Yes, this is an online course I am taking. cs50.h provides the students with a string type. I don't know why they would do this, considering string type doesn't exist in C – howdydoody Feb 11 '14 at 16:21
  • 1
    try at least doing `printf debugging` to find out where does this Segmentation Fault occur and *then* come to us for help. – zoska Feb 11 '14 at 16:23
  • @ryyker: `string` here is a typedef in the cs50 header (`typedef char * string`). To the OP: `GetString` returns `NULL` in some cases... do check for that, too. Also: avoid that `string` type if possible: `GetString` allocates the memory on the heap, so you're best off `free`-ing the pointer if you're done playing with it. – Elias Van Ootegem Feb 11 '14 at 16:30
  • show your command line and input string. `if(argc == 2 && argc == isalpha(argv[1]))` It does not crashed difficult to break through. – BLUEPIXY Feb 11 '14 at 16:36
  • I removed isalpha(argv[1])) and the program is now running. It isn't correct but is running. Now I have some debugging to do to the encryption part of this assignment. Thanks for the help – howdydoody Feb 11 '14 at 16:44
  • again show your command line and input string. – BLUEPIXY Feb 11 '14 at 16:53
  • @howdydoody: don't forget to add `free(text)` and `return 0` to the end of your main function... you `int main`<-- means it should return an int. Yours returns `void`: check the compiler warnings – Elias Van Ootegem Feb 11 '14 at 17:12

3 Answers3

3

Also working through this exact tutorial at the moment and encountered the same error. I believe the issue is that you are using isalpha(argv[1]) on a string whereas I think isalpha only works on a single character. I've ended up using a for loop to run through the argv[1] keyword character by character using argv[1][i].

Very new to this so I hope I haven't steered you in the wrong direction.

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

should be

int main(int argc, char *argv[])  

There is no string data type in C. Your compiler should raise a warning. cs50.h will not gonna work until you download and link to project cs50.c file.

Also, isalpha expects argument of type int but in your if's condition

if(argc == 2 && argc == isalpha(argv[1])) {...}  

you are passing a pointer to string, which is wrong.

AsElias Van pointed that Getstring allocates memory on heap, you need to free your pointer after using it.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    It is dubious that it even built that way. – ryyker Feb 11 '14 at 16:15
  • cs50.h has (for whatever odd reason) `typedef char* string;` – nos Feb 11 '14 at 16:17
  • 2
    Chances are high that this mysterious `` typedefs `string` (which is a reserved identifier anyway) as `char *`, which is mindlessly foolish obfuscation. – Crowman Feb 11 '14 at 16:17
  • 2
    I mean, this is the least problem. It's difficult to spot even a single "correct" line in this code :( – FtM Feb 11 '14 at 16:19
  • @FtM - OP has declared he is new to C. (for whatever that's worth) – ryyker Feb 11 '14 at 16:22
  • @PaulGriffiths _mindlessly foolish obfuscation_ love it. But "Chances are high that..." What?? – ryyker Feb 11 '14 at 16:23
  • @ryyker he apparently is new to programming in general. Nothing wrong here, but is very difficult to write a complete, decent answer when there are so many errors on the basics: the only thing I can say is to study more and try simpler things! – FtM Feb 11 '14 at 16:26
  • 1
    Might be worth adding to this answer that the `GetString` function allocates _heap memory_, so people who use this lib might not be aware of that fact, and fail to `free` the memory after using their _"string"_ (cf [source of cs50.c here](http://d2o9nyf4hwsci4.cloudfront.net/2010/fall/lectures/5/src/cs50.c)) – Elias Van Ootegem Feb 11 '14 at 16:35
  • he is including so it will compile without a problem. – alfredocambera Jan 08 '15 at 04:18
  • @alfredocambera; What are you talking about? – haccks Jan 08 '15 at 10:44
  • @haccks I'm referring to your answer about the usage of "int main(int argc, string argv[])". You are saying that the compiler should show a warning message but is not the case, because he is including the appropriate library. The real problem here is that, as user3271692 pointed on the next answer, that he is using a function that receives a char on a string. – alfredocambera Jan 08 '15 at 11:50
  • @hackks You are saying "you are passing a pointer to string, which is wrong. ". If you are including "string.h", the main function can use string as a second parameter. As I ferered on my previous comment the compiler should not raise any warnings while using string and including the string.h library. Elias Van Ootegem is correct when he says that the method reserves memory. But that is not the problem. The problem is simple: isalpha receives as parameter a character (int), but howdydoody is passing an string. – alfredocambera Jan 08 '15 at 15:39
  • @alfredocambera; What is the difference when saying passing a string and passing a pointer to string? – haccks Jan 08 '15 at 16:42
  • @hackks What I'm trying to explain is that your current answer is not completely accurate. "int main(int argc, string argv[])" doesn't have to be replace by "int main(int argc, char *argv[])". Both method calls are correct. – alfredocambera Jan 08 '15 at 18:39
  • @alfredocambera; Did you read the complete answer? It says: *There is no string data type in C. Your compiler should raise a warning. `cs50.h` will not gonna work until you download and link to project `cs50.c` file.* – haccks Jan 08 '15 at 19:11
  • I'm referring to the first three lines: "int main(int argc, string argv[]) should be int main(int argc, char *argv[]) " – alfredocambera Jan 08 '15 at 19:48
  • @alfredocambera; OK. Could you explain the reason behind this waring in [this code](http://ideone.com/2xCdDX) ? – haccks Jan 08 '15 at 19:56
  • @haccks You're right regarding the ch50.h library being required for a successful compilation. The problem was not that he needed cs50.h, and that can be proved due to the fact that occurred a segmentation fault. SegFaults occur during runtime. You are referring a compilation error while howdydoody was talking about a running time error caused by the string being passed to the isalpha() function. Because of that, I consider that the answer given by user3271692 is the correct one. – alfredocambera Jan 08 '15 at 21:53
  • @alfredocambera; Thanks. Good to know that you got the point :) – haccks Jan 08 '15 at 22:02
1

Try looping through argv[1] to access the characters that make up the string i.e. argv[1][s].

Then use isalpha on the actual characters individually i.e. isalpha(argv[1][s]) as opposed to using it on the entire string.

I used the following function to perform this task

// if checker returns 0 then it's all alphabet
int checker(string key)
{
    for(int i = 0, len = strlen(key); i < len; i++)
    {
        if(isalpha(key[i]) == 0)
        {
            return 1;
        }
    }
    return 0;
}
andromeda
  • 4,433
  • 5
  • 32
  • 42