Fibonacci code

I am trying to write a code to calculate the nth number in a Fibonacci sequence, where the user enters n in the program interactively.

I have written this code so far, but I have errors when I build and I'm not sure why

#include <iostream>
int main()
{
int x, i, j, n, m;
cout <<"Enter the total number \n";
cin >>n;
cout <<"Total number of the series is \n"<< n;
i=0,j=1;
cout <<i<<j;
for(x=3;x<=n;x++)
{m=i+j;
cout <<"\n"<<m;
i=j;j=m;
}
return 0;
}


Amy help would be appreciated
i=0,j=1; is wrong. should be semicolon instead of comma:

i=0;
j=1;

also, to ensure consistency in the output, it will be better if you include a line character between the initial i and j as well:
cout <<"\n" << i << "\n" << j;
Actually i=0,j=0; is valid, works as intended, and is more clear at a glance as to what it does.
Last edited on
Thanks for the help,
However when I build the new code the

cout <<"Enter the total number \n";
cin >>n;

part says that there is errors in the "cout" and "cin" part because they are undefined. Any advice?
You need to write std::cout and std::cin.
Alternatively you can use a standard namespace. At the top of your code, type

using namespace std;

That way you do not have to use std::cout or std::cin for every cout and cin you use.
@CodingKid that technique is deprecated, please do not recommend it.
closed account (Dy7SLyTq)
or you can do using std::cout etc
The method mentioned by @DTSCode is preferable over the one mentioned by @CodingKid.
closed account (Dy7SLyTq)
albeit a little longer
Verbosity is preferred in programming. Even Stroustrup, the creator of C++, asks programmers to be more verbose with their code.
I think it's a little trivial pointing out something as simple as using namespaces when it is a simple program such as writing fibonacci sequences.

I can understand when using it in large programs, but what is the harm in placing it in a program that is only approximately 20 lines long?
Thanks. The code successfully builds, but when I start debugging and enter a value, it says that the "j" variable is being used without being initialized?
closed account (Dy7SLyTq)
@coding kid: because its the same as system(). people get into the habit of using it in trivial code and without thinking use that in bigger and bigger programs

@looping bird: plz post ur code
@DTSCode

#include <iostream>
using namespace std;
int main()
{
int x, i, j, n, m;
cout <<"Enter the total number \n";
cin >>n;
cout <<"Total number of the series is \n"<< n;
i=0;
cout <<"\n" << i << "\n" << j;
j=1;
cout <<i<<j;
for(x=3;x<=n;x++)
{m=i+j;
cout <<"\n"<<m;
i=j;j=m;
}
return 0;
}
put j = 1 after line i = 1 and you probably should remove line cout << i<<j; you did almost the same thing with cout << "\n"<<i<<"\n"<<j;
I don't like the idea of this loop which starts from x=3.
for(x=3;x<=n;x++)

See if you can modify your code so that it works like this:
for (int x=0; x<n; x++)
The code now runs fine, but when I press enter a value when I am debugging the window disappears
Before return 0; you'll want to use this code:

1
2
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Be sure to #include <limits>;

Alternatively you can use this before return 0:

 
cin.get();
I now have this code

#include <iostream>
using namespace std;
int main()
{
int x, i, j, n, m;
cout <<"Enter the total number \n";
cin >>n;
cout <<"Total number of the series is \n"<< n;
i=0;
j=1;
cout <<"\n" << i << "\n" << j;
for(x=0;x<=n;x++)
{m=i+j;
cout <<"\n"<<m;
i=j;j=m;
}

cin.get();

return 0;
}

But when I enter a value once I have debugged, the window closes
Topic archived. No new replies allowed.