1

When checking the command line argument in Vigenere.c to see if it contains any non-alphabetic characters, I can't get my for loop to iterate past the first character. I have read all of the posts on here for the past 4 hours and still have not gotten anywhere. Please note that the code posted is not an attempt at the whole program, rather just trying to get the first step done correctly!

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("You didn't enter one plaintext keyword!\n");
    return 1;
}

else
{
    for (int i = 0; i < strlen(argv[1]); i++)
    {

        if (!isalpha (argv[1][i]))
        {
            printf("Your keyword contained characters other than letters.\n");
            return 1;
        }
        else
        {
            printf("Alpha\n");
            return 0;
        }    
    }   
}
}
3

Both clauses of the if/else contain return statements. When a return statement is encountered, it will immediately return control to the calling program with no further processing in the current function. Since this is in main, it is terminating the program and returning control to the operating system. The code will never go any farther than the if/else.

When a non-alpha is detected, you want to terminate, so that return is correct. Else, you want to continue testing, so remove the second return statement.

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

2
  • I don't think that answers my question. If I run with command line statement 'a1', it prints "Alpha". If I run with command line statement '1a', it prints "Your keyword contained...". I don't see how the return line affects if a1 is used. It simply seems like the loop is not checking past the first char. If the code under the else statement is replaced with //do some stuff will the if statement check all of the chars in argv[1]? Aug 20 '16 at 23:10
  • 1
    In each case that you stated, the printf statement will execute. The next line is a return, which terminates main. When the return statement is executed, control is returned to the operating system and no further code in main will be executed. If you really want to test it, comment out both return statements and use a1b2c3xyz567 and look at the output. Then uncomment one of the return statements. Then switch - comment that statement out again and uncomment the other return statement.
    – Cliff B
    Aug 20 '16 at 23:19

You must log in to answer this question.

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