I am trying to use fairly simple C++ code to complete this problem: The user will input a number of the Fibonacci series, and through a function called whichfib(), the program will then output the number using long double data type.

I have gotten the code to work until the number 47, some numbers after 47 will be negative, and some are just completely off. The code should only work from fib numbers 0-95, here is my code:

``````#include <iostream>
#include <iomanip>
using namespace std;

long double whichfib (int num);

int main ()
{
int num, count=-1;

while (count < 0 || count > 96)
{
cout << "Which Fibonacci number would you like? ";
cin >> num;
count = num;
}
cout << setprecision(0);
cout << "Fibonacci #" << num << " is " << fixed << noshowpoint << whichfib (num) << endl;
return 0;
}

long double whichfib (int num)
{
int count=2, newnum1, newnum2, newnum;
long double fnum;

if (num == 0)
fnum = 0;

else if (num == 1)
fnum = 1;

else if (num == 2)
fnum = 1;

else
{
newnum1=0;
newnum2=1;

do
{
newnum = newnum1 + newnum2;
newnum1 = newnum2;
newnum2 = newnum;
count++;
}
while (count < num);

fnum = newnum1 + newnum2;
}
return fnum;
}``````

if anyone can help it would be greatly appreciated, but please remember this is a beginner course, I cannot put any advanced statements, it must be a while statement and also just what is available in the iostream library. Thanks again

3
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by bleonard989

Welcome aboard..hope you'll enjoy it here. Your formatting is horrible...please look at the two anouncements at the top of the page, so you can place your code in tags. Also, in the future, before posting, search the forums to see if someone else had the same problem/assignment. We are willing to help, but we don't want to waste time doing the same thing over and over again...here are a few links that may be able to help you:

Yeah, im sure that it is horrible, this is the introductory course, and our teacher isnt too worried about the formatting, just as long as it works. I have searched the forums and all of the answers given direct to stuff that we have not learned yet, so I cannot use any of these methods. Those two links you posted came up broken for me.

I'm not talking about the formatting for your class...i'm talking about formatting the code for the forum....again...read the two announcements at the top of the page...

Your problem lies in the intermediate variables you use in the whichfib( ) function:

``````long double whichfib (int num)
{
int count=2, newnum1, newnum2, newnum;
long double fnum;``````

Your use of long double is good for dealing with the large values that will result. You should also use that type for the newnum variables. Remember, at each step, these variables grow at the same rate as your final value - they get BIG. The int will blow up at 2 billion and change, well short of your desired answer range. If you used unsigned int, you'd get 4 billion and change, still too small. All the variables other than count should be long doubles.

And, just for fun, keep in mind that long double is not a consistent sized variable on all systems. In Windows, using MS Visual C++, long doubles are 8 bytes. Most other compilers will use 10 bytes.

Val

ok thanks for the help guys, I got it figured out today

``````long double whichfib (int num)
{
int count=1;
long double newnum, newnum1, newnum2;

if (num == 0)
newnum = 0;

else if (num == 1)
newnum = 1;

else
{
newnum1=0;
newnum2=1;

do
{
newnum = newnum1 + newnum2;
newnum1 = newnum2;
newnum2 = newnum;
count++;
}
while (count < num);

}
return newnum;
}``````