Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Need help with compsci assignment (C)

  1. Oct 28, 2006 #1
    the assignment wants me to count the number of people in age groups and to report the number of people in each group.

    i came up with this, used an int for each group and incremented with if/else if statements.

    Code (Text):
          int x, infant, young, middleaged, old, older, reallyold;
          infant = 0;
          young = 0;
          middleaged = 0;
          old = 0;
          older = 0;
          reallyold = 0;
          printf("Please enter ages (Enter -1 to quit): ");
          scanf("%d", x);
          while (x != -1)
          if (x <= 16)
          else if (x <= 29)
          else if (x <= 55)
          else if (x <= 75)
          else if (x <= 99)
          else if (x > 99)
          printf("Number of infants: %d \n", infant);
          printf("Number of young people: %d \n", young);
          printf("Number of middle aged people: %d \n", middleaged);
          printf("Number of old people: %d \n", old);
          printf("Number of older people: %d \n", older);
          printf("Number of really old people: %d \n", reallyold);

    though i'm not sure if its exactly right.. and it won't let me compile.

    i keep getting the "unexpected end of file" error. please help? :)

    i also want to make it so that you can enter as many ages as you want, but i'm not sure what kind of loop i should use for that.
    Last edited: Oct 28, 2006
  2. jcsd
  3. Oct 28, 2006 #2


    User Avatar
    Science Advisor

    You should move the lines
    printf("Please enter ages (Enter -1 to quit): ");
    scanf("%d", x);
    to just inside the while loop, not just outside it. Then you'll also have to initialize x to something other than -1. The reason it's not compiling is you forgot the closing curly brace } on the while loop.

    Also you should use proper indentation. The while keyword itself should be the same indentation as the stuff before it, and the contents of the loop should be indented one more. Also if you have an "if" statement, the body of the statement should be indented one more than the "if" statement, like.
    Code (Text):

    while X
        if A
        else if C
        else if G
        other stuff here
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook