Hi, I get infinite loops when I enter a digit which is not expected from scanf function while programming a standalone application for the retired version of zybo. what I want is to enter just an integer. I used the next code to avoid entering something that is not an integer by accident, as an alternative for scanf. This works great on CodeBlocks, but when I try to use it on SDK it does not work, it's like I cannot enter anything. How can I solve this issue? should I use another function or library?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p, s[100];
int n;
while(1)
{
while (fgets(s, sizeof(s), stdin)) {
n = strtol(s, &p, 10);
if (p == s || *p != '\n') {
printf("Please enter an integer: ");
} else break;
printf("You entered: %d\n", n);
}
printf("the number is %d\n",n);
Question
enriqeat
Hi, I get infinite loops when I enter a digit which is not expected from scanf function while programming a standalone application for the retired version of zybo. what I want is to enter just an integer. I used the next code to avoid entering something that is not an integer by accident, as an alternative for scanf. This works great on CodeBlocks, but when I try to use it on SDK it does not work, it's like I cannot enter anything. How can I solve this issue? should I use another function or library?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *p, s[100];
int n;
while(1)
{
while (fgets(s, sizeof(s), stdin)) {
n = strtol(s, &p, 10);
if (p == s || *p != '\n') {
printf("Please enter an integer: ");
} else break;
printf("You entered: %d\n", n);
}
printf("the number is %d\n",n);
}
}
Link to comment
Share on other sites
1 answer to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.