0

I really struggled to get this to work, but I ultimately found a way. (I'm aware it is not terribly elegant.) Anyway... All I have left is that I need to make it so that if the passcode is not alphabetic, it returns an error. Every time I try to do what makes the most sense to me (or any variation) I get a segmentation fault. I'm not looking for this to be solved for me, necessarily, as I don't want to cheat. I just would like some help as to what I am doing wrong so I can understand what to do right.

This is my most recent attempt. Without "&& isalpha(key)" the code works perfectly, except it won't error on like "./vigenere 123" or whatever. Anyway. Any help would be appreciated. Thanks!

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

// Implements Vigenere Cipher
int main(int argc, string argv[])
{
    int j;
    string key = argv[1];
    if (argc == 2 && isalpha(key))
    {
     ...
[EDIT: unrelated code removed for Honor Code compliance.]
0

It's a common problem. Well, two problems. First, isalpha() is designed to take a single char. This code tries to shove an entire string down isalpha's throat and it chokes on it, generating a seg fault. You need to step through the string with a for loop, checking each individual char.

Second, it will also get a seg fault if there is no key. The code assigns argv[1] to key before it checks whether it even exists.

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

1
  • Hi I am having a same problem. My question is how to check argv[1]? I used the same code for caesar problem and it works but for vigenere, it keeps saying "null pointer passed as argument 1, which is declared to never be null /usr/include/string.h:400:33: note: nonnull attribute specified here" – Issa Jul 18 '18 at 17:59

You must log in to answer this question.

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