branded_ann
branded_ann

Reputation: 1

Why won't this for loop execute?

I'm making a simple program that gets the users balance from the entered ID. For some reason the for loop doesn't execute when an ID from the listof_ID array is entered:

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

int main()
{
    int cntr;
    int ID;

    int listof_ID[] = {001, 002, 003, 004, 005};
    float listof_BAL[] = {338.90, 745.87, 897.32, 665.36, 102.45};

    puts("**Check Your Balance** \n");
    printf("**Please Enter Your ID >> ");
    scanf("%d", &ID);
    for(cntr = 0; cntr > 5; cntr++)
    {
        if(ID == listof_ID[cntr])
        {
            puts("Your balance is ");
            printf("%.2f", listof_BAL[cntr]);
            break;
        }
    }

    return 0;
}

Upvotes: 0

Views: 113

Answers (2)

programmingIsFun
programmingIsFun

Reputation: 1067

In your for loop, the first value of cntr = 0, which means that cntr < 5. But in the condition, you are checking if cntr > 5, which is not true. So, the loop does not start.

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60681

for(cntr = 0; cntr > 5; cntr++)

Should be

for(cntr = 0; cntr < 5; cntr++)

The loop executes while the condition cntr > 5 is true. If cntr starts at 0 then it is obviously not greater than 5, so the body of the loop never executes.

Upvotes: 7

Related Questions