I am trying to do a postfix evaluation using stack. The output is supposed to be in a format 2,3,4,+,-,# with # signifying the end of expression. The problem is negative numbers are allowed i.e 2,3,-10,+,-,# is also a valid input. I need a method to separate the numbers from the characters.
int main() {
int num;
char ch;
int ret;
printf("Enter integers or characters separated by commas (e.g., 12,a,34,#):\n");
while (1) {
scanf(" ,");
ret = scanf("%d", &num);
if (ret == 1) {
printf("Input is an Integer: %d\n", num);
if (ret == -1)
break;
} else {
if (scanf(" %c", &ch) == 1) {
// Print an error message for non-integer input
//ch = getchar();
printf("Input is a Character: '%c' (invalid integer)\n", ch);
} else {
printf("End of input\n");
break;
}
}
}
return 0;
}
The problem I am facing is, if the input is 1,2,3,a,b,-1 -> it correctly identifies the numbers as numbers and a,b as characters. But if I give 1,2,3,+,-,#,-1 as input, it somehow takes , instead of + and - while recognizes #. What can I try next?
The problem is scanf("%d", &num) only has one character look ahead. It reads the + sign and since that could start a number, it reads the next character to parse the number, but this character is a , so it pushes it back to the input stream and returns 0. Your version of scanf cannot push back more than one byte so the next byte read by scanf(" %c", &ch) is the ,, not the + sign. The same problem occurs for the - after the ,.
This behavior looks like a quality of implementation issue, but the C Standard actually specifies that a prefix of the expected form is read and dicarded, only the first byte that cannot be part of the match is left in the input stream. Hence the + sign and any preceding white space are consumed for the failing %d conversion. One more pitfall in the scanf family of functions that makes them difficult to use properly and makes error recovery unlikely.
scanf() is not the right tool for your project. fgets and strtol seem more appropriate and provide better error checking:
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char buf[200];
printf("Enter integers or characters separated by commas (e.g., 12,a,34,#):\n");
while (fgets(buf, sizeof buf, stdin)) {
char *p;
char *q;
long num;
char ch;
for (p = buf; *p; p++) {
if (isspace((unsigned char)*p)
continue;
errno = 0;
num = strtol(p, &q, 10);
if (q > p) {
if (errno) {
printf("Input is an out of bound integer: %.*s\n", (int)(q - p), p);
} else {
printf("Input is an integer: %ld\n", num);
}
p = q;
} else {
char ch = *p++;
printf("Input is a character: %c\n", ch);
if (ch == '#')
break;
}
while (isspace((unsigned char)*p))
p++;
if (*p == ',')
continue;
printf("expected a comma\n");
if (*p == '\0')
break;
}
}
return 0;
}
The issue is that the characters + and - may be part of a number, such as in -1, so in the case of these characters, when scanf("%d", &num) fails to read an integer decimal number it will consume the + or - characters from the input, therefore the following scanf(" %c", &ch) ends up reading the separating comma as the input character.
One solution to this would be to first read each entry between commas , as a string and store it in a char array. And to do that you can use the specifier " %[^,\n]" in the scanf function, which is the string specifier to discard leading blank characters, and store anything that is not a comma , or a new line character \n in the char array.
And then you can use the sscanf function to process the string in the array, to find out if it is a valid int or not.
Like this:
#include <stdio.h>
#include <string.h>
int main() {
char string[100] = "";
char ch = '\0';
int num = 0;
int ret = 0;
while (scanf(" %99[^,\n]", string) == 1) {
if (sscanf(string, "%d", &num) == 1) {
printf("Input is an Integer: %d\n", num);
}
else {
printf("Input is Not an Integer: '%s'\n", string);
if (strcmp(string, "#") == 0) break;
}
ret = scanf("%c", &ch);
if (ret != 1 || ch == '\n') break;
}
printf("End of input\n");
return 0;
}
Or, as suggested by @chux-ReinstateMonica, you could use strtol to process the string, so that you can also handle out of range input values, like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
int main() {
char string[100] = "";
char ch = '\0';
char *end = NULL;
long num = 0;
int ret = 0;
while (scanf(" %99[^,\n]", string) == 1) {
errno = 0;
num = strtol(string, &end, 10);
if (string < end && *end == '\0' && errno != ERANGE){
printf("Input is an Integer: %ld\n", num);
}
else if (errno == ERANGE) {
printf("Input is Out of Range: '%s' (Ranges from %ld to %ld)\n", string, LONG_MIN, LONG_MAX);
errno = 0;
}
else {
printf("Input is Not an Integer: '%s'\n", string);
if (strcmp(string, "#") == 0) break;
}
ret = scanf("%c", &ch);
if (ret != 1 || ch == '\n') break;
}
printf("End of input\n");
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With