# Thread: Fibbonacci Code

1. ## Fibbonacci Code

Hey all, Programming newbie here.

I'm programming the Fibbonacci code, but I can't figure out how to set the condition

"if NOT EQUAL to any numerical value"

This is what I have so far:

Code:
```#include <stdio.h>
#include <conio.h>

long fib(int);

int main()
{
int n;

printf("Enter the Fibbonacci number you would like to view: ");
scanf("%i", &n);

if (WHAT GOES HERE?)
{
printf("Input error.\n");
getch();
}
else if (n<0) {
printf("Fibonacci is undefined for negative integers.\n");
getch();
}
else {
printf("The %i-th Fibonacci number is %ld\n", n, fib(n));
}
getch();
return 0;
}

long fib(int n)
{
if (n == 1 || n == 2)
{
return 1;
}
else
{
return (fib(n-1) + fib(n-2));
}
}```
Thanks for the help in advance!

Steel.

2. This should be on the C board, not C++ as that code is written in pure C.

Code:
`conio.h`
this is non-standard, read the FAQ to find out other ways to stop the console window closing, dependant on your IDE, getchar() is usually best.

3. Originally Posted by swgh
This should be on the C board, not C++ as that code is written in pure C.

Code:
`conio.h`
this is non-standard, read the FAQ to find out other ways to stop the console window closing, dependant on your IDE, getchar() is usually best.
Ok, thanks for the help, I'll re-post this in the C Board. Sorry for the inconvenience.

4. Most of us'd replace the scanf() with fgets(), then use strtol() to extract the number with error-checking.

Code:
```char buffer[BUFSIZ];
char *endp;

fgets(buffer, BUFSIZ, stdin);
n = strtol(buffer, &endp, 10);
if (endp == buffer)
printf("Input error\n")```

5. Integer can only be numerical... I don't know if it is 0 when a string is entered or it is something else...

6. Thanks for the reply. I've only been coding for a Computer science course for about a week now, and I've never heard of those functions (although i'm certain their more efficent). Is there any way I can keep the remainder of my code intact, only changing what's in the
if (...) structure? Such as creating a condition where "all non-integers" output "input error".

Thanks

7. Originally Posted by zx-1
Use logical-OR operator. (||)
I've tried that, using

Code:
`if ( (n<0) ||  (n>0) )`
Yet even all inputs, even valid, ourput "invalid output"

8. Carry on here - Fibbonacci Code

Popular pages Recent additions