Question

I am wondering why is it that regardless if I input a digit or a letter or a character it keeps printing the last print line, how come the yes "This is an integer line never work? How do I go about getting the user to only input a positive digit?

  #include <stdio.h>
  #include <limits.h>

  main(){
  unsigned int num;
  printf("Please input a positive number: ");

  scanf("%d",&num);
  if ((num >= 'a' && num <= 'z') || (num >='A' && num <= 'Z')){
    printf("not an interger(alpha)");
  }
  else if ( num >= '0' && num <= '9'){
    printf("yes this is an integer");
  }
  else{
    printf("not an integer");

  }
}
Was it helpful?

Solution 3

You need to get the types of your variables right. An unsigned int can only ever be that: an non-negative integer. You cannot look at the first character in an integer.

Your program analyses a string, i.e. an array of characters, even if it only looks at the first letter. Therefore, your data must be an array of characters. This also means that you need to scan for a string with "%s".

When you test the first character of your string, you must use an array index, str[0].

To put all this together:

#include <stdio.h>

int main()
{
    char str[20];

    printf("Please input a positive number: ");
    scanf("%19s", str);

    if ((str[0] >= 'a' && str[0] <= 'z') || (str[0] >='A' && str[0] <= 'Z')) {
        printf("alpha\n");
    } else if (str[0] >= '0' && str[0] <= '9') {
        printf("integer\n");
    } else {
        printf("other\n");
    }

    return 0;
}

Edit: As pointed out, this code does not check whether a string repesents a decimal number. It only checks the first character. A full solution should check all characters and is left as an exercise to the original poster.

My impression is that the OP has difficulties to distinguish between the data types, so I tried to focus on that.

I've also added a field width to the scanf to avoid buffer overflow. I'm not going into the details of sscanf here, because I think that the OP still has to understand more of the very basic stuff.

OTHER TIPS

change else if ( num >= '0' && num <= '9') to else if ( num >= 0 && num <= 9), assume you input a number between 0 and 9.

You are not comparing against int in second if. Depending upon your need, change it to (i.e. remove ' ')

(num >= 0)

or

else if ( num >= 0 && num <= 9)

The ASCII of '0' is 48. and '9' is 48+9. so else if ( num >= '0' && num <= '9') will be true if you input a value between 48 and 57.

The solution is, as jfly said, change to else if ( num >= 0 && num <= 9).

This code is incorrect. Please check out the manual page for scanf http://linux.die.net/man/3/scanf

You need scanf("%c", &character_var); and call it repeatedly. Alternatively read a string

EDIT

if (scanf("%u", &num) == 1) {
   printf("Well done - you managed to type in a positive integer %u\n", num);
} else {
   printf("Have another go!\n");
}

Is a solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top