| tidbit (4) | |||
|
Hello, I have been trying to count the number of digits in an integer. I know I am close, but I am not quite sure what I am doing wrong. Could someone please point me in the right direction?
My output is always 1 for A. I know I am just misunderstanding something simple. Thank you in advance for any help on this problem. | |||
|
|
|||
| bluezor (298) | |
| If you use an if loop, it will only execute once if the condition is true. Then it won't execute again. | |
|
|
|
| mogha (10) | |
|
Try the below code snippet #include <iostream> using namespace std; int main() { long A; int n=0; cout << "enter value: " ; cin >> A; cin.ignore(); while(A) { ++n; A /= 10; } cout << "entered value is " << n << "digit long" << endl; } | |
|
|
|
| jsmith (5804) | |
|
mogha: so in other words the number zero has zero digits. (we avoid just giving solutions to people since they don't typically learn anything that way). | |
|
|
|