Hi, I actually have two questions about this code. One is in the subject line. I want to basically read in a string and make sure it is an integer which works fine. But then I want to cast it to int so I can actually use it. I know that what I have only returns the pointer but I don't know how to get the actual value. I have tried many things and always get "segmentation fault". My other question is about fgets. The second loop has an "fgets" but it skips it the first time for some reason, which is why I have that flag=0 so it goes through the loop again and then it works fine. I've tried using fflush(stdin) in several places but always the same thing.
Code:
#include <stdio.h>
#include "math2.h"
int main(void)
{
int i,x,flag,z;
char *y, c[BUFSIZ];
y=c;
do {
printf("Please enter a 0 or 1: ");
scanf("%d",&x);
} while (x!=0&&x!=1);
do {
printf("\nPlease enter a number to evaluate: ");
fflush(stdin);
fgets(y, BUFSIZ, stdin);
flag=0;
for(i=0;i<strlen(y)-1;i++) {
if (*(y+i)<'0' || *(y+i)>'9') {
flag=0;
break;
} else {
flag=1;
}
}
} while (flag==0);
if (x==0) {
printf("\nThe factorial of %s is: %d\n",y,fact((int)*y));
} else {
printf("\nThe fibonaccia sequence is %d\n",fibonacci((int)*y));
}
exit(0);
}
Thanks for any help you can provide.