c++ cin value returning 0 instead of the input value
c++ cin value returning 0 instead of the input value
Hi I am extremely new to c++. Working on an assignment where I need to add up all numbers of input then display the numbers next to eachother with the sum.
example:
input: 1234, output : 1 2 3 4 10
Here is my code so far:
#include <iostream>
using namespace std;
int main()
int myNum;
int total = 0;
int digit;
cout << "Enter a number" << endl;
cin >> myNum;
while(myNum >0)
digit =myNum %10;
myNum/=10;
total += digit;
while(myNum <0)
digit =myNum %10;
myNum /=10;
total +=digit;
cout << "The sum of digit is:" << myNum << total << endl;
return 0;
The second while loop was to deal with negative numbers, but down on the cout
when I put myNum
to print the value I input it just prints 0
in front of the total, any reason the values aren't being carried over or how I could go about getting it to carry over?
cout
myNum
0
myNum
cout
0
Tip: instead of repeating the digit summing logic for negative and positive numbers, you can use the
abs
function to find the value-without-negative-sign.– Cheers and hth. - Alf
Sep 17 '18 at 2:12
abs
You can use strings and make them
char array
and then parse any char to integer and find the sum of them this is easier from that.– Azhy
Sep 17 '18 at 2:14
char array
Why are you printing the value of
myNum
at the end, which will always be 0, instead of the TOTAL value that you've diligently calculated?– Sam Varshavchik
Sep 17 '18 at 2:16
myNum
In with your development environment you will almost always find a debugging tool you can use to control the execution of the program. With a debugger you can step through your program instruction by instruction and inspect the variables to see what effect the instruction had. This can make finding or narrowing down the location of logical errors much easier.
– user4581301
Sep 17 '18 at 2:44
2 Answers
2
I updated the code to meet your original requirement. input:1234 output 1 2 3 4 10
The original code printed the myNum
variable after using the /= operator repeatedly. The result will always be zero.
myNum
The requirements specify that each digit of the input must be printed as well as the total. In order to save the intermediate results, a vector is introduced. As each digit is produced it is pushed into the vector.
Once the input is exhausted, the individual digits and the total can be printed. The vector is traversed in reverse to print the digits in left to right order.
using namespace std;
int main(int arg, char*argv)
int myNum;
vector<int> digits;
int total = 0;
int digit;
cout << "Enter a number" << endl;
cin >> myNum;
while(myNum >0)
digit =myNum %10;
myNum/=10;
total += digit;
digits.push_back(digit);
while(myNum <0)
digit =myNum %10;
myNum /=10;
total +=digit;
digits.push_back(digit);
for (auto it = digits.rbegin(); it != digits.rend(); ++it)
cout << *it << " ";
cout << total << endl;
return 0;
It will be useful to explain why the OP's code did not work and why your solution works.
– R Sahu
Sep 17 '18 at 2:38
Done, good advice, thanks
– Matthew Fisher
Sep 17 '18 at 2:48
Alternative: You could immediately print the input back out again as well as summing it, eliminating both the
vector
and the iteration loop at the end.– user4581301
Sep 17 '18 at 3:02
vector
I'm certain there are many improvements possible to the OP's algorithm. The code was meant to allow the OP to compare and consider the defects of the original. The requirements are vague but there appears to be spaces in output: 1 2 3 4 so just printing the input would not be quite right.
– Matthew Fisher
Sep 17 '18 at 3:08
@AidanAgee Accepting the answer would be appreciated. push_back adds an element to a vector, see details and examples here en.cppreference.com/w/cpp/container/vector
– Matthew Fisher
Sep 17 '18 at 3:15
No need to use a container if the sequence of digits is not required strictly in the same order.
You can simply print them as you go...
#include <iostream>
using namespace std;
int main()
int myNum;
int total = 0;
int digit;
cout << "Enter a number" << endl;
cin >> myNum;
cout << "The sum of digits : ";
while(myNum > 0)
digit =myNum %10;
myNum/=10;
total += digit;
cout << digit << " ";
while(myNum < 0)
digit =myNum %10;
myNum /=10;
total +=digit;
cout << digit << " ";
cout << "is : " << total << endl;
return 0;
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
The program logic guarantees that
myNum
is 0 at the end. And so when you askcout
to output it, you get0
. I would just remove that.– Cheers and hth. - Alf
Sep 17 '18 at 2:11