Not sure what is happening

Every time I run the program it just never prints out after I put in input. the goal is to add up every digit of a larger number and provide the exact sum, found similar but not exact questions on forums.

For example, i will enter in "123" in to the input line, then nothing happens. Just blank.



Last edited on
it appears to loop forever.

do
{
adder = user1 % 10; //does not change user1.
sum += adder; //does not change user1.
}
while (user1 != 0); //user1 still not zero? keep going until it changes... which it never will.
You've forgot to initialize the sum variable in the AddDigits function.

The do-while loop runs until user1 is zero, but user1 is never modified within the loop which means that if user1 is not zero before the loop it will run forever.

Note that the variables in main is not the same as the variables in AddDigits even if you use the same names. If you want the sum variable in main, that you have declared on line 15, to hold the value that AddDigits returns you will have to assign to it.
 
sum = AddDigits(user1);

after changing adder to simply user1, still nothing changed. Same result of nothing
@Peter87 I changed it to
 
int sum = 0;

still nothing chagnes.
i also changed this
1
2
user1 = user % 10;
sum += user1; 
Last edited on
Do you understand what the % operator does and why you are using it here?
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
#include <iostream>

void AddDigits();

int main()
{
	AddDigits();
	return 0;
}

void AddDigits()
{
	int user1;
	int sum = 0;
	do
	{
		std::cout << "Please enter an integer (0 to quit): " << std::endl;
		std::cin >> user1;
		sum += user1;
	} while (user1 != 0);
	if (user1 == 0)
	{
		std::cout << "The sum of the digits is " << sum << std::endl;
		std::cout << "Goodbye." << std::endl;
	}
}

Last edited on
@Peter87 i’m aware of what it does. divide and put out the remainder. it’s separating the digits for me.

whoever did that code that was not at all what i was trying to do.
What you might be looking at is an array
Give an example of what the question is and what is the result you are expecting
alldaynodae wrote:
@Peter87 i’m aware of what it does. divide and put out the remainder. it’s separating the digits for me.

Well, user1 % 10 only gives you the rightmost digit.

If user1 is 123 and you do user1 = user1 % 10; then user1 will get assigned the value 3.
Next time this code is executed user1 will again be assigned the value 3 it's still the rightmost digit.
And like that it continues forever...

What you had in your original code was almost correct. The only thing that you forgot to do was to remove the rightmost digit from user1 in each iteration of the loop. This can be done using the division operator.
Last edited on
@Peter87 How would i use the division operator? (no attitude intended i promise) thank you for your helpe
Imagine this as a mathematical problem, if you ignored any decimal part, what number would you divide with to get rid of the rightmost digit?

Note that in C++ when you divide two integers the decimal part is simply lost.
i believe dividing by the other integers? i’m sorry if i seem dumb on this lol, i’m quite confused on it
1234 / 10 is 123.
1234 % 10 is 4.

that is the easy one.
how do you get 3?
1234 % 100 is 34. 34/10 is 3.
1234 % 1000 is 234. 234 /100 is 2

see a pattern that you could use in a loop here?
i see a pattern, but i’m not sure how i would implement that into a do while loop then adding each digit together
Wait could i just mod the user1 then assign that to its own variable then in the do while loop after doing the mod operator and just using the division to reassign the user1 variable by 10 each time it runs through that function? until the initial digit represents a value of 0?
Yes, that sounds like the right approach.
Appreciate the help, like a lot. But I have a new question Im about to post. This one is a bit more hefty.
Topic archived. No new replies allowed.