# Quick newbie question :)

• 10-20-2005
Cdrwolfe
Quick newbie question :)
Well my first post here, off to an embaressing start, but you can't get far with out asking some questions, and this one i hope is simple enough.

Just started C++ and was having a go at writing this particular algorithm, after getting somewhere but really nowhere, i got the real deal someone else made and tried to jury rig some of his code into mine to get it to work (i know i know :)).

well here we go, the Good professional version

Code:

``` #include <iostream> using std::cout; using std::endl; using namespace std;     int main(int argc, char* argv[]) {   int iCount = 1;   int iNo = 22;   int iTemp  = iNo; {   while (iTemp != 1)   {             if (iTemp % 2 == 0)       {         iTemp /= 2;       }             else       {         iTemp = iTemp * 3 + 1;       }       ++iCount;   }   cout << "Cycle length of " << iNo << " is " << iCount << endl;     cin.get();   return 0; }  }```
and my version

Code:

```#include <iostream>        using namespace std;                 int main(int argc, char* argv[])                         // Most important part of the program! {                           // Need a variable...   int count = 1;   int N;   int X = N;            cout<<"Input Number: ";         cin>> N;           cin.ignore();    while (X != 1)   {   if (X % 2 == 0)   {     X /= 2; }   else   {     X = X * 3 + 1;       }     ++count;  }        cout << " cycle length of " << X << " is " << count << endl;       cin.get();       return 0; }```
where the good version gives the correct output, cycle length of 22 is 16.

while mine gives for any number inputed, cycle length of 1 is 255

i orginally didn't have int X = N; but considering the other had int itemp = ino; i added it anyway.

well there you go feel free to laugh, i just was interested if anyone can figure out why it doesn't work :(.

Regards Wolfe
• 10-20-2005
Daved
The original code outputs iNo, not iTemp, yet you output X instead of N?

Also, you assign X to N, but you do that before N has any value, so X is just filled with garbage.
• 10-20-2005
Cdrwolfe
thank you will try it now.

edit: thanks nice to see where i went wrong, thought becuase i put X=N, that "cycle length of" X would output the original value for N that ws typed.

Thanks again for the help
• 10-20-2005
Enahs
Well, for starters your X = N;…it is bad. You are setting X equal to N before N is initialized N to anything(I am surprised your compiler let you compile it).
Just move it down a couple lines after N has had its value input by user.

Then it will work just fine, but your output will be wrong because you output X as the original number instead of N. Just change the X to N in the last cout line you have.

*edit*
Bah! I was beat while typing!
• 10-20-2005
Cdrwolfe
Thanks, works now.

going to try and see if i can get it to perform it on numbers 1 through 10 then output each result on que.