Fibonacci Numbers? Help on while loop

Having trouble with the while loop in this code. I'm trying to get the 9th term of the Fibonacci numbers. For instance if I input 1 for number1 and number2, then input the number of which term from the sequence I want from the Fibonacci number, in my case I did 9. All I get then is just a blank cursor on the next line.

Anyone know whats wrong with my loop??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  	int _tmain(int argc, _TCHAR* argv[])
{
	//declare two variabes
	int number1;
	int number2;
	int nextnumber;
	int counter=2;
	//Declare the variable for which numbered sequence the user wants

	int whatsequenceuserwants;
	//Ask user for two numbers
	cout<<"\n\tNumber 1: ";
	cin>>number1;
	cout<<"\n\tNumber 2: ";
	cin >> number2;
	//Ask the user for what term of number they want
	cout<<"\n\n\tWhat term from the sequence do you want? ";
	cin>>whatsequenceuserwants;
	//formula for finding the next number

	if (whatsequenceuserwants==1)
	{cout<<"First Number: " <<number1;}

	else if (whatsequenceuserwants==2)
	{cout<<"Second Number: "<< number2;}
	
			while (whatsequenceuserwants>=3 && counter==whatsequenceuserwants)
				{nextnumber= number1+number2;
						if(whatsequenceuserwants==counter)
							cout<<"Number is" <<nextnumber;
							nextnumber=number1;
							number1=number2;
									counter++;
	}
	_getch();
	return 0;
}

From what I can see in order to satisfy your while loop requirements whatsequenceuserwants needs to be greater than or equal to 3 and counter needs to be equal to whatsequenceuserwants. This can't happen because you set counter = 2 and whatsequenceuserwants needs to be 3 or greater, so 2 != 3.
I understand what your saying :) I have changed my code a little; however, when I want to find the position of the 9th Fibonacci number, all I get is

"Number is2"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int _tmain(int argc, _TCHAR* argv[])
{
	//declare two variabes
	int number1;
	int number2;
	int nextnumber;
	int counter=3;
	//Declare the variable for which numbered sequence the user wants

	int whatsequenceuserwants;
	//Ask user for two numbers
	cout<<"\n\tNumber 1: ";
	cin>>number1;
	cout<<"\n\tNumber 2: ";
	cin >> number2;
	//Ask the user for what term of number they want
	cout<<"\n\n\tWhat sequence of number do you want? ";
	cin>>whatsequenceuserwants;
	//formula for finding the next number

	if (whatsequenceuserwants==1)
	{cout<<"First Number: " <<number1;}

	else if (whatsequenceuserwants==2)
	{cout<<"Second Number: "<< number2;}
	
			while (counter<=whatsequenceuserwants)
				{nextnumber= number1+number2;
			
						if(whatsequenceuserwants==counter)
							cout<<"Number is" <<nextnumber;
							nextnumber=number1;
							number1=number2;
							counter++;
									
	}
	_getch();
	return 0;

Topic archived. No new replies allowed.