0

I've just started the vigenere task, and so far implemented only the starting point - a check, whether the keyword input is alphabetical. It works as intended, when the keyword starts with a non-alphabetical char, in all other cases it looks like the check is not working. What is the problem here?

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

int main (int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Don't joke with me, cowboy! I said enter the name of the programm AND one keyword ONLY. Try again!");
        return 1;
    }

    string keyword = argv[1];
    for (int i = 0, l = strlen(keyword); i < l; i++)
    {
        if (isalpha(keyword[i]))
        {
        return 0;
        }
        else
        {
            printf("Looks like you've entered an invalid keyword. Remeber, your keyword must contain letter only. Try again!");
        return 1;
        }
    }
    printf("Enter the text you want to cipher: \n");
}

1 Answer 1

1
    if (isalpha(keyword[i]))
    {
    return 0;

Ummm.... is there a reason you want to end the program with a return statement when the first char is a letter?

As written, this code is going to end the program as soon as it looks at the first char, either by returning a 1 or a 0. It will never get past the first char, and it definitely won't get past the first pass of the for loop.

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

1
  • What a stupid mistake :( I see it now, thank you very much. Oct 1, 2016 at 8:41

You must log in to answer this question.

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