0

I am stuck on validating the user input for the key phrase in (there aren't a lot of questions on here about that, so I am guessing I am missing something rather obvious...).

I was trying to use !isalpha() and after learning that I cannot pass a string into the function, I tried using a for loop to iterate through the char's in the key phrase. However, my program won't compile, giving me the error statement:

"expected expression"

(with the little green ^ pointing at the beginning of the for loop).

My suspicion is that I've got the syntax wrong, but I have not been able to find any resource on using for loops as a condition inside an if statement (maybe this just does't work?).

Looking at my code, can anyone give me a hint as to why I am seeing this error or point to toward a more elegant solution for user input validation?

Many thanks!

vigenere.c

int main (int argc, string argv[])
{
    // check user input
    if (argc !=2)
    {
        printf("please follow command ./vigenere with a key word (letters only)\n");
        return 1;
    }

    else if (
        for (int h = 0, n = strlen(argv[1]); h < n; h++)
        {
            if ( !isalpha(argv[1][h]) )
        }
    )
    {
        printf("please follow command ./vigenere with a key word (letters only)\n");
        return 1;
    }

1 Answer 1

0

You haven't structured the statements correctly. an IF statement must have the following structure:

if( <test condition> )  <single statement or code block in curly braces> ;

You have an opening parenthesis followed by a for statement instead of a test condition and a closing parenthesis. Then you have another if statement with a test condition, but no closing semicolon or paired curly braces.

You would benefit greatly from reviewing the class lectures and shorts on constructing if statements, for loops, and the use of parentheses and curly braces.

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

2
  • Thanks for answering! Just to clarify, a test condition can only ever be a simple boolean statement and not a loop of several tests? I.e. I cannot iterate through several conditions inside the if statement and if any one of them fails the if statement returns false (instead of going condition 1 || condition 2 || condition 3 || and so on...)? Thanks!
    – Greg
    Oct 2, 2015 at 14:16
  • Actually, just the opposite. A test condition can be as simple or as complex as you want. The big requirement is that no matter how complex the test, it must evaluate to be either true or false. For example, usingif ( a || b || c || d || e ) if any one of the variables a through e is true, then the whole test is true. Or, if( a && b && c && d && e ) every one of a through e must be true for the test to be true. I could give more examples, but you get the idea.
    – Cliff B
    Oct 2, 2015 at 15:59

You must log in to answer this question.

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