I've ran into a problem :(

Something is wrong between my functions. When I try to take 'num' from the main function and put it in the getFib() function, it turns into some crazy number, sometimes random. Why wont it stay the same number that was declared in main?

Sorry I know its a mess, it was nice and neat until I tore into it trying to find the problem. I placed some cout statements for debugging but no luck.

Any help would be appreciated, thank you.

#include <iostream>
using namespace std;

long double getFib(long double);

int main()
{         

        long double num = 98;
        while ( num < 0 || num > 97 )
        {
              cout << "Which Fibonacci Number Would You Like? ";
              cin >> num;
        }       
        
        long double b;
        getFib(b);
        
    cout << "Cout Num:" << num << endl;
    cout << "Fibonacci #" << num << " is " << b;
    
    system ("pause");
    return 0;
}

long double getFib(long double num)
{
    long double a = 0, b = 1, c=0;
    cout << "Num: " << num << endl;
    cout << "Before: " << a << b << c <<endl;
    for (long double i = 0; i <= num; i++) 
    {
        long double c = a + b;
        a = b;
        b = c;
    }           
    cout << "After: " << a << b << c << endl;
return b;
}

Well... what seems to be wrong with these 2 lines:

long double b;
getFib(b);

Nothing that I can see. But since I am new to C++ I dont have a trained eye. I have tried changing everything from int, long int, long double etc.

But the main problem I have is that when num goes to getFib() it becomes a very strange number thats nothing close to what I input to main.

B hasnt been the problem of my main concern.

Since b is unitialized, the value of b could be *anything.*

>But the main problem I have is that when num goes to getFib() it becomes a very strange number thats nothing close to what I input to main.

And where exactly does num go into getFib() ?

Since b is unitialized, the value of b could be *anything.*

>But the main problem I have is that when num goes to getFib() it becomes a very strange number thats nothing close to what I input to main.

And where exactly does num go into getFib() ?

Is it not getting its value from the function getFib()?

I was under the impression that it was, I had create a variable b in main() so that the program would compile as I'm not allowed to use global functions.

EDIT: Didnt see you edited your post, before I submitted this one.

Let me recompile this real quick.

Do you know what a scope is?

Your getFib() function can't magically get variable values from your main function.

And where exactly does num go into getFib() ?

Im under the impression that it goes into the parentheses. Right?

(Main Fnct left out)

long double getFib(long double num) [B]// Num goes in here??[/B]
{
    long double a = 0, b = 1, c=0;
    cout << "Num: " << num << endl;
    cout << "Before: " << a << b << c <<endl;
    for (long double i = 0; i <= num; i++) 
    {
        long double c = a + b;
        a = b;
        b = c;
    }           
    cout << "After: " << a << b << c << endl;
    return b;
}

>Num goes in here??

If num goes there, why did you pass b , an uninitialized variable with a random value?

OK, let's look at this thing logically

int main()
{         

        long double num = 98;
        while ( num < 0 || num > 97 )
        {
              cout << "Which Fibonacci Number Would You Like? ";
              cin >> num;
        }       
        // you just read your FIBB number into **num**

        long double b;    // you just created **b** which contains an unknown value
        getFib(b);        // now you call your function with the unknown value
                          // what do you want to do with **num**??
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.