Code continues to repeat

My first output keeps repeating (i.e. number = 24 | output= 00000000000000....
Could someone please explain it to me. I have had the same problem when using loops on several occasions.

Thank you.

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
  #include <iostream>

using namespace std;

int number;
int integer;
int counter = 1;

int main ()
{
	cout << "Enter an integer:";
	cin >> number;
	
	while (number != 1)
	{
		if (0 < (number % 2))
			(number * 3) +1;
		else
			number = integer / 2;
		
		cout << number;
		counter ++;
	}
	cout << "K = " << counter <<endl;
	
	system("pause");
	return 0;
}
First, what's this program doing ?

If you see,
1
2
3
4
if (0 < (number % 2))
	(number * 3) +1;
else
	number = integer / 2;

the integer is not assigned, some compilers will let it be 0.
and so you used while (number != 1) but you have nowhere change number's value, so if you don't input 1, there'll always be infinity loop.

I think you missed assigning integer's value somewhere and have to change
1
2
if (0 < (number % 2))
	(number * 3) +1;

, that's all.
Last edited on
CORRECTION: I was able to resolve the problem, thanks so much.

The point of the program is to use a sequence depending on odd or even to find the last value to be 1. Which is why I am using a sentinel-controlled loop.
Last edited on
Topic archived. No new replies allowed.