can any1 pls tell me how can i solve my problem?
i'm running a c program that required the user to key in a int, but instead of keying in int the user key in char... and somehow the program stor the char n turn it into int...
some even continue looping... how can i solve it... demanding for help...

Dani AI

Generated

The root cause is not that C magically turns letters into meaningful ints, but that input functions and conversions behave in predictable ways: char is an integer type (as noted), atoi() silently converts or fails (as warned), and scanf("%d",...) will leave non-digits in the input buffer and return 0, which often makes a loop appear to “hang.” was right to read a line and validate it first. A robust pattern is: read a whole line with fgets(), parse with strtol() (so you can detect errors), check errno/endptr, ensure there is no extra junk after the number, and check the value fits in int.

Example pattern:

/* include <stdio.h> <stdlib.h> <errno.h> <limits.h> <ctype.h> */
char buf[128];
long val;
char *end;

if (fgets(buf, sizeof buf, stdin) == NULL) { /* handle EOF */ }

errno = 0;
val = strtol(buf, &end, 10);
if (errno == ERANGE) { /* overflow/underflow */ }
else if (end == buf) { /* no digits found */ }
else {
    while (isspace((unsigned char)*end)) end++;
    if (*end != '\0' && *end != '\n') { /* trailing non-space junk */ }
    else if (val < INT_MIN || val > INT_MAX) { /* out of int range */ }
    else {
        int result = (int)val; /* valid */
    }
}

Why this helps: strtol() gives an endptr so you can tell whether any digits were read and whether extra characters remain; it sets errno on overflow. Avoid atoi() for validation—see atoi(3) and prefer strtol(3). Also avoid using scanf() for interactive validation; when it fails it leaves input in stdin (see scanf(3)). Small extras: cast to unsigned char before isspace()/isdigit(), handle empty lines and EOF, and print clear prompts when retrying.

Recommended Answers

All 6 Replies

char is in fact a (very small) integer type. To convert the string of char to int, use atoi() or something similar.

char is in fact a (very small) integer type. To convert the string of char to int, use atoi() or something similar.

sorry but how to use the atoi() as i'm a new in this c programming...

i've tried to limit it with the if else but it just work in simple function but not in the complete main

You must first read in your text book how to code in C. This is not a good place to learn the very most basic things, but a place to come if you have written a program that is not working right, or maybe does not compile.

If you want to prevent someone from entering anything other than numeric digits I would get then to enter a string instead of int, then check the string for digits.

char input[255] = {0}; // input buffer
char *ptr;
printf("Enter a number\n");
fgets(input,sizeof(input),stdin); // get keyboard 
// now strip off the trailing '\n' that fgets() add to the end of the string
ptr = strrchr(input,'\n');
if( ptr != NULL)
   *ptr = '\0';
// now check each character for digits. 
// If any non-digits are found then loop 
// back to the top of this code and to it again

thanks for that... already got it now... ^_^

char is in fact a (very small) integer type. To convert the string of char to int, use atoi() or something similar.

From atoi(3)
"
DESCRIPTION
The atoi() function converts the initial portion of the string pointed to by nptr
to int. The behaviour is the same as

strtol(nptr, (char **)NULL, 10);

except that atoi() does not detect errors.

"

So as you recommended, "something similar" (strtol) is better than atoi(). Especially given that OP wants to find errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.