help with homework assignment

why does my program keep freezing at the point where it is suppose to implement the while statement?



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

using namespace std;

int main()
{
	int num, num2, sum = 0;
	int div, num3, count = 0;

	cout << "Enter an integer: ";
	cin >> num;
	cout << endl;                    // This is the freezing point.

	count = 0;

	while (num != 0)
	{
		num2 = num / 10;
		count++;
	}

	num == num2;

	do
	{
		div = static_cast<int>(pow(10, --count));
		num3 = num2 / div;
		num2 = div % div;

		cout << num3 << ' ';
		sum = sum + num3;
	}

	while (count != 0);
	cout << "The sum of the integers is: " << sum
		<< endl;
	
	return 0;
}
Last edited on
Line 22: What do you think this line does? == is the equality operator. Did you mean to do an assignment here (=)?

Line 34: The ; terminates the while statement.

Line 26: If you're taking 10 to a negative power, the result is going to be a fraction (<1). Assigning the result to an int is going to result in 0.

Line 27: Guaranteed you're going to attempt to divide by 0 here.

Line 28: Modulo of a number by itself will always be 0.

Line 26: You use the pow() function, but don;t include the <cmath> header.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


1
2
3
4
5
while (num != 0)
{
num2 = num / 10;
count++;
}


When will this loop finish?
I honestly don't understand the concept of the equation, or really for that fact, the concept of how to implement the for loop to solve the equation. Furthermore, the LCV concept is eluding me also. What I do understand is that the LCV is the condition that keeps the loop from going on endlessly, but I'm just not catching on to how to actually write one out. For example, in the while loop
1
2
3
 while (num != 0) 
{ num2 - num  / 10;  
count++}


I just cant come up with the correct algorithm to eventually make it false.
Last edited on
Topic archived. No new replies allowed.