0

I'm trying to create a function which will evaluate whether or not the user's command line input is a letter. The function returns an integer, which should be zero if the string is all alphabet characters and will be more than zero if anything else is true. In the function there is a for loop.

My problem is that the for loop is not executing at all. The lines outside the for loop will run and the ones inside the for loop will not.

Source code:

int eval(string k)
{

    // for loop scans input string k for items
    for(int i = 0; i > strlen(k); i++)
    {
        // do something
    }       

    return evalstatus;
}

Anything I'm missing would be much appreciated.

2
  • 1
    As side note, you may want to put strlen(k) into another variable since you don't want it to get called every time you iterate over your loop, consider using: for (int i = 0, j = strlen(k); i > j; ++i) instead.
    – user103853
    Commented Aug 17, 2014 at 11:24
  • Could you please give us some input you have tried and the output you have gotten?
    – user103853
    Commented Aug 17, 2014 at 11:26

1 Answer 1

1

In your code, the test condition for the for loop should go like this

    for (int i = 0; i < strlen(k); i++)

because when you will pass the string to the function, then it will be of non-zero length(in most cases), say any positive integer. Now when you compare this with the condition, i > strlen(k) at first time, then it is actually 0 > positive number condition, which is always false. Hence, loop does not execute. Therefore, you need to change the condition to i < strlen(k).

You must log in to answer this question.

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