Fibbonacci Code

This is a discussion on Fibbonacci Code within the C++ Programming forums, part of the General Programming Boards category; Hey all, Programming newbie here. I'm programming the Fibbonacci code, but I can't figure out how to set the condition ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    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.
    Last edited by SilverSteel; 09-20-2006 at 01:53 PM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    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. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote 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. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    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")
    Last edited by Ken Fitlike; 09-22-2006 at 04:23 PM. Reason: made code visible
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Integer can only be numerical... I don't know if it is 0 when a string is entered or it is something else...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Quote 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. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Carry on here - Fibbonacci Code
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21