What is wrong with this program, loop endless and "evens", "odds" output dummy values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • expoC
    New Member
    • Apr 2010
    • 1

    What is wrong with this program, loop endless and "evens", "odds" output dummy values

    Why is the below program is not coming out of the for loop and the variables outputting dummy values

    Code:
    /* oddeven.c: Illustrates looping construts */
    #include <stdio.h>
    
    int main() {
        for (; ;) {
            int num;
            int evens=0;
            int odds=0;
            puts("Enter integer: ");
            scanf("%d", &num);
            switch(num % 2){
            case 0:
                 evens = evens + 1;
                 break;
            case 1:
               odds = odds + 1;
               break;
            }
            printf("evens: %d Odds: %d\n", &evens, &odds);
            if (evens + odds == 10)
                break;
          
        }
        return 0;
    }
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Because the for loop does not have an end condition and int num has not been initialized.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      because you reset cound for evens and odds every iteration - they never reach 10. And also you have prinf done wrong ( &odds, not odds ).

      Comment

      Working...