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.
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;}
elseif (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.
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;}
elseif (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;