need assistance with Age Counter program

okay so I'm just starting to learn Turbo C.. My professor assigned us to make a simple program that counts ages, (0-18 is infant, 19-29 is young, 30-50 is middle aged, 51-69 is old, 70+ is really old..)

anyway I was able to make a program, i got it down but I wanted to add a command that lists all the ages and how many are in that group like this when I press 'l':

There are:
## infants
## young aged
... and so on

I did get it to do that, problem is I have to press it twice to give the list and ENTER key twice to be able to enter another age.. I can't figure out how to make it so that you only have to press 'l' once to reveal the list and ENTER once to be able to input another age.

so here's what i've done so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
main()
{
 int age, b, c, d, e, f;
 clrscr();
 b=0;
 c=0;
 d=0;
 e=0;
 f=0;
 do
  {
   if(getch()=='l')
    printf("There are:\n%d Infants\n%d Young aged\n%d Middle aged\n%d Old aged\n%d Really old aged\n", b, c, d, e, f);
   else
    {
     printf("Please enter an age: ");
     scanf("%d", &age);
     if(age>=0 && age <=18)
      {
       b=b+1;
       printf("1 Infant added.\n<ENTER> to input another age, 'l' for a list\n of ages or 'q' to quit.\n");
      }
      else
       if(age>=19 && age<=29)
	{
	 c=c+1;	 
         printf("1 Young aged added.\n<ENTER> to input another age, 'l' for a list\n of ages or 'q' to quit.\n");
	}
       else
	if(age>=30 && age<=50)
	 {
	  d=d+1;
	  printf("1 Middle aged added.\n<ENTER> to input another age, 'l' for a list\n of ages or 'q' to quit.\n");
	 }
	else
	 if(age>=51 && age<=69)
	  {
	   e=e+1;
	   printf("1 Old aged added.\n<ENTER> to input another age, 'l' for a list\n of ages or 'q' to quit.\n");
	  }
	 else
	  if(age>=70)
	   {
 	    f=f+1;
	    printf("1 Really Old aged added.\n<ENTER> to input another age, 'l' for a list\n of ages or 'q' to quit.\n");
	   }
    }
  }
  while(getch() != 'q');
}


any help would be appreciated.. Thank you!

Please note that I'm very new to this so you might have to explain a little. Thanks again!
Oh dear not getch() again.... getch() is not part of the standard c++ language and it is a part of conio.h library. I do not see a conio.h included. Try to use something like cin.get or cin.getline instead.
http://www.cplusplus.com/reference/iostream/istream/getline/
http://www.cplusplus.com/reference/iostream/istream/get/
After doing that you may also need to include iostream. Also I think clrscr() only works with borland's and conio.h. What that means is it is compiler specific, old, and non-standard.
Try reading this to see a few other ways to clear the screen:
http://www.cplusplus.com/forum/beginner/1988/page3.html

--- Edit: I also noticed that your program starts off completely blank in the console no prompt to tell the user anything. I had to press a key to make it move on. Probably caused because of getch, but i never use that so it is hard for me to say.

--- Second Edit: Turbo C? Oh dear.... That is old and was really great back in the day, but why not try getting a newer IDE that allows you to program the actual c++ language of today?
Last edited on
Topic archived. No new replies allowed.