-5

I'm trying to build a program in C that:

  1. Gets age of user
  2. Checks if age is between 18 to 120
  3. Checks if age doesn't contain other characters such as letters, dots and so.
  4. If it isn't between 18-120 or contains other characters go back to section 1.

To check number 3 I believe that I need to scan the age from the user as a char but then i cant check if it's between 18 to 120. I can't use arrays or strings. This is the code that I have for now which checks that code doesn't contain other characters:

void main() {
char age;
int error = 0;
do
{
    error = 0;
    printf("Please enter your age:");
    scanf("%c", &name);
    while (name != '\n')
    {
        if ((name<'0') || (name>'9')){
            error++;
        }
        name = getchar();
    }
} while (error != 0);
}  
Adam Morad
  • 167
  • 1
  • 7
  • Nice Homework :D Do it yourself! (If it's not homework you haven't put effort into it) – Rizier123 Dec 01 '14 at 15:33
  • It's not clear that you've even succeedded at step 1. Perhaps you should show what you've done so far. – Bill Lynch Dec 01 '14 at 15:33
  • Get it as a char array (C style string), then use `strtol` to convert to a number. As a bonus `strtol` will give you a pointer to any illegal character it finds. – Ben Voigt Dec 01 '14 at 15:34
  • It makes sense to perform step 3 before step 2. – chux - Reinstate Monica Dec 01 '14 at 15:38
  • As I said, can't use arrays or strings, and I know how to check 2 and 3 individually but not together: if i define age as an int i can check 2, and if i define age as a char i can check 3. but how do i do both? – Adam Morad Dec 01 '14 at 15:59
  • Keep `age` and the `char` input separate: e.g. `int ch = fgetc(stdin)`. If the `ch` is non-digit, note error. Else accumulate age with `age = age*10 + ch-'0'`. Test if `age > 120`. After reading the entire line, test if `age < 18`. – chux - Reinstate Monica Dec 01 '14 at 16:11

2 Answers2

1

Keep age and input in separate variables like age and ch.
As each ch is read, test for EOF, '\n' and if it is a digit. OP's code has done some of that.
If it passes, accumulate the age. Test for the maximum value (120). This will also detect overflow.
After the input line is processed, check for the minimum value (18).

#include <ctype.h>
#include <stdio.h>

int main(void) {
  int error;
  int ch;
  do {
    int age = 0;
    error = 0;
    printf("Please enter your age:");
    fflush(stdout);
    while ((ch = fgetc(stdin)) != EOF && ch != '\n') {
      // Maybe also throw out white-space
      if (isspace(ch))
        continue;
      // or if (!isdigit(ch)) {
      if ((ch < '0') || (ch > '9')) {
        error = 1;
      } else {
        age = age * 10 + ch - '0';
        if (age > 120)
          error = 2;
      }
    }
    if (age < 18)
      error = 3;
    printf("Age: %d Error: %d\n", age, error);
  } while (error && ch != EOF);
  return 0;
}

Important to use int ch rather than char ch.

Minor bits: Use correct signature. Example:

 // void main()
 int main(void)

When using scan(), always good to check return value. In this situation, code can simple use fgetc() or getchar().

Code posted does not compile. (name is not defined.) Unless you are having compilation problem, always post compile-able code.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You should use the long int strtol(const char *nptr, char **endptr, int base) function, read the manual.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97