I am new to C++. I am teaching myself using a friends textbook. There is an assignment at the end of the control structures (repetition) chapter in my text book. It asks for you to write a program that asks a user to input a number. Then write code that will separate that number into individual integers and produce the sum of all the integers that are part of the number that was input.
for example: 123 is output as 1 2 3. and the sum is 6.
My code is below. The issue I am having involves the while function that separates each digit and prints them out individually. Whatever I try, the output is always backwards. For example, if the input is 123, the output is
3 2 1. I want the output to be 1 2 3.
I have tried a counter control loop, I have tried a for loop. Because the input is a single number consisting of multiple integers, I am having trouble understanding how to use a counter when technically you only have one input.
(bare with me, my understanding of C++ is still very basic) What I think needs to happen is somehow, when the numbers are separated, re-input each separated integer into a "variable" so that each integer can be counted and that would make it easier to output 1 2 3 instead of 3 2 1, if the input was 123. Issue is I don't know how to do this.
If you step through what this is doing... the code is isolating the last digit of the number first.
temp = 123
123%10 = 3 ( the modulus gives you the remainder of the division of 123/10)
123/= 10 (temp is now 12)
temp = 12
12%10 = 2 (again, the remainder after the division)
12/10 = 1 (temp is now 1)
temp = 1
1%10 = 1
1/10 = 0 (temp is now 0 so loop will exit)
Does the problem specify the length of the number entered? There are different ways to split out the digits. This is one way to split out the digits without a loop (this assumes a 5 digit number entered.)
1 2 3 4 5 6 7 8
cout << “Please enter a 5 digit integer: “ ;
cin >> number ;
cout << number / 10000 << “, “;
cout << number % 10000 / 1000 << “, “;
cout << number % 1000 / 100 << “, “;
cout << number % 100 / 10 << “, “;
cout << number % 10 << endl;
What I think needs to happen is somehow, when the numbers are separated, re-input each separated integer into a "variable" so that each integer can be counted and that would make it easier to output 1 2 3 instead of 3 2 1, if the input was 123. Issue is I don't know how to do this.
Perhaps the easiest way to do this is store each digit into an array.
1 2 3 4 5 6 7 8 9 10 11 12
int digits[5]; // Assumes no more than 5 digits
int num_digits = 0;
while(temp)
{ // Least significant digit in digits[0] etc
digits[num_digits++] = temp % 10;
temp /= 10;
}
// Then loop through displaying the digits
for (int i=num_digits-1; i>= 0; i--)
cout << digits[i] << " ";
cout << endl;
PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes your code easier to read and also easier to respond to.
wildblue, thanks for your input, according to the exercise from the book, there is no input limit for the length of a number, but what you suggested as a solution is solid. The only other way to accomplish this would be to use an array, but so far I have not learned that, and the exercise does not want me to use an array. I can use your suggestion to modify the solution and get the result I am looking for.
AbstractionAnon,
Thanks for your suggestion. This was not in the description of my question, but I have not learned about arrays yet and the exercise did not ask us to use an array. What you have suggested I am excited to learn because it seemingly makes life that much easier.
Thanks,
Ddrake44
p.s.
I will use code tags when posting future code.