0

I created a program to make a diamond out of *'s. I am looking for a way to check if the type of input is an integer in the C language. If the input is not an integer I would like it to print a message.

This is what I have thus far:

if(scanf("%i", &n) != 1)
   printf("must enter integer");

However it does not display the message if it's not an integer. Any help/guidance with this issue would be greatly appreciated!

apxcode
  • 7,696
  • 7
  • 30
  • 41
user3418175
  • 23
  • 1
  • 1
  • 4

5 Answers5

1

you can scan your input in a string then check its characters one by one, this example displays result :

  1. 0 if it's not digit
  2. 1 if it is digit

you can play with it to make your desired output

char n[10];
int i=0;
scanf("%s",  n);

while(n[i] != '\0')
{
printf("%d", isdigit(n[i]));
i++;
}

Example:

#include <stdio.h>
#include <string.h>

main()
{
    char n[10];
    int i=0, flag=1;
   scanf("%s",  n);

   while(n[i] != '\0'){
   flag = isdigit(n[i]);
   if (!flag) break;
   i++;
   }

   if(flag) 
   { 
       i=atoi(n);
       printf("%d", i);
   }
   else
   {
       printf("it's not integer");
   }

}
chouaib
  • 2,763
  • 5
  • 20
  • 35
1

Use fgets() followed by strtol() or sscanf(..."%d"...).

Robust code needs to handle IO and parsing issues. IMO, these are best done separately.

char buf[50];
fgets(buf, sizeof buf, stdin);
int n;
int end = 0; // use to note end of scanning and catch trailing junk
if (sscanf(buf, "%d %n", &n, &end) != 1 || buf[end] != '\0') {
  printf("must enter integer");
}
else {
  good_input(n);
}

Note:

strtol() is a better approach, but a few more steps are needed. Example

Additional error checks include testing the result of fgets() and insuring the range of n is reasonable for the code.

Note:

Avoid mixing fgets() and scanf() in the same code.
{ I said scanf() here and not sscanf(). }
Recommend not to use scanf() at all.

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

The returned endPtr will point past the last character used in the conversion.

Though this does require using something like fgets to retrieve the input string.

Personal preference is that scanf is for machine generated input not human generated.

dennis
  • 221
  • 1
  • 5
0

Try adding

fflush(stdout);

after the printf. Alternatively, have the printf output a string ending in \n.

Assuming this has been done, the code you've posted actually would display the message if and only if an integer was not entered. You don't need to replace this line with fgets or anything.

If it really seems to be not working as you expect, the problem must be elsewhere. For example, perhaps there are characters left in the buffer from input prior to this line. Please post a complete program that shows the problem, along with the input you gave.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

Try:

#include <stdio.h>
#define MAX_LEN 64

int main(void)
   { bool act = true;
     char input_string[MAX_LEN];  /* character array to store the string */
     int i;

     printf("Enter a string:\n");
     fgets(input_string,sizeof(input_string),stdin);  /* read the string */


     /* print the string by printing each element of the array */
     for(i=0; input_string[i] != 10; i++) // \0 = 10 = new line feed
        { //the number in each digits can be only 0-9.[ASCII 48-57]
          if (input_string[i] >= 48 and input_string[i] <= 57)
             continue;
          else //must include newline feed 
             { act = false; //0
               break;
             }
        }

    if (act == false)
       printf("\nTHIS IS NOT INTEGER!"); 

    else
       printf("\nTHIS IS INTEGER");

    return 0;
   }

[===>] First we received input using fgets.Then it's will start pulling each digits out from input(starting from digits 0) to check whether it's number 0-9 or not[ASCII 48-57],if it successful looping and non is characters -- boolean variable 'act' still remain true.Thus returning it's integer.