I'm having a little difficulty, trying to transition from c++ to objective-c. I tried to write a fairly simple Fibbonacci sequence computer, which I usually write to familiarize myself with new languages. Unfortunately, it doesn't work. I keep getting errors related to either comparison between an int and a pointer, or about missing an lvalue increment operand.

I'm really frustrated!

Can someone please help? Pretty please?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
     int input[0], fib = 0, bib = 1, Total = 1, i[0];

     printf("How many numbers? =>  ");
     scanf("%d", input);

     for (*i = 0; i < input; *i++)
         {
             printf("%d", Total);
             Total = fib + bib;
             fib = bib;
             bib = Total;
          }

    return 0;
}

I am not sure why you have declared input and i as arrays, this seems wrong. and the * before i in two spots on line 11 should not be there. an int is a primitive of the language and not referenced with a pointer.

Thanks for catching that! What a horrible mashup of syntax I had going on here! Oh, the downfalls of a newbie trying to learn 6 different languages at once...

I finally solved the primary problem myself with a little research on proper fscan implementation. On line 9 of my original code, I tried to assign a value to an integer (ok, an array, in this example. But it was supposed to be an integer, and it was an integer before I "fixed" it the first few times), but fscan only likes to deal with pointers.

The simple fix for this is to add a '&', as if dereferencing a pointer, and fscan is happy enough with this that it leaves me alone.

Here is my corrected, functional code:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // Declared values
    int input = 0, fib = 0, bib = 1, Total = 1;

    // User input asked for.
    printf("How many numbers would you like to calculate?  ");
    scanf("%d", &input);    // User input given.

    // This loop calculates/prints the Fibonacci sequence.
    for (int i = 0; i < input; i++)
    {
        printf("%d\n", Total);
        Total = fib + bib;
        fib = bib;
        bib = Total;
    }

    return 0;
}

Edited 4 Years Ago by sinatra87

This article has been dead for over six months. Start a new discussion instead.