0

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;
}
6
  • 5
    cntr > 5 will not happen since it is initialized to 0 Commented Aug 15, 2012 at 20:36
  • 5
    Do you realize that you are initializing listof_ID[] with octal literals? In this case it won't matter, but still, it seems as though you are probably unaware of this. Commented Aug 15, 2012 at 20:38
  • @EdS. I wasn't sure if that was the case or not since the number is preceded by two zeros instead of the usual one. Commented Aug 15, 2012 at 20:40
  • 1
    @TheZ: std::cout << 0010; // prints "8". All it takes is one leading zero, the rest are irrelevant. Commented Aug 15, 2012 at 20:46
  • 1
    @EdS. Showing versus telling, I like it! +1 Commented Aug 15, 2012 at 20:49

2 Answers 2

7
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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.