5
#include <iostream>
#include <string>


using namespace std;

int count_number_place(int number)
  {

    int number_placement;

    while (number >= 1)
     {
       number_placement++;
       cout << number_placement <<endl;
       number/=10;
     }

    return  number_placement;
  }



int main(int argc, const char * argv[]) 
  {
    // insert code here...

    int user_input_number;

    cout << "Please enter your number here" << endl;
    cin >> user_input_number;
    cout << "User input number is "<< user_input_number <<endl;
    cout << "The numbers of digits in the input number is :" << count_number_place(user_input_number) << endl;

    return 0;
  }

I'm trying to create a small program that calculates the number of digits of a given number.
Whenever I type in numbers like 200 the expected results is 3. Instead I got 7963. When I put a breakpoint at the line number_placement I got a default value of 7961 which is weird because that value wasn't assigned anywhere in the code.

Can you please explain why I get that result?

Arun A S
  • 6,421
  • 4
  • 29
  • 43
user3526002
  • 537
  • 2
  • 8
  • 19
  • 3
    `int number_placement = 0;` You never initialize it, so it starts with some garbage value. – BoBTFish Apr 08 '15 at 08:02
  • That's just unintialised garbage stack allocated memory without explicit assigned values will be initialised to garbage – EdChum Apr 08 '15 at 08:02
  • 1
    As BobTFish has pointed out you never initialise the variable so what are you expecting here? – EdChum Apr 08 '15 at 08:03
  • It is uninitialized, therefore as others say. It collects garbage from somewhere. But this code isn't valid either. So try to initialize it with 0 or if the problem persists, post correct code. – jPO Apr 08 '15 at 08:03

2 Answers2

5

Just change your function to

int count_number_place(int number)
  {

    int number_placement = 0;         // assign 0

    while (number >= 1)
      {
        number_placement++;
        cout << number_placement <<endl;
        number/=10;
      }

    return  number_placement;
  }

That is, change

int number_placement;

to

int number_placement = 0; 

If you try to access uninitialized variables, you will get garbage values because it is undefined behavior. The compiler will just give it some garbage values.

This link might be useful

What happens to a declared, uninitialized variable in C? Does it have a value?

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
3

When the program is compiled, the variable you created (in this case, number_placement), will have some memory space allocated to it (a position in memory, which has an address). If you try to access this address (this space I talked about), and you didn't assign anything to it before, you will read what was left there, it's current value. We call it garbage, because it's leftover memory from some other application that could have been using this memory position before, or just some memory that didn't have any value assigned to it before since the memory was powered on (or corrupted because the memory didn't refresh this position because it was unused).

Always initialise your variables.

And by that I mean: Don't read values from variables that you didn't assign a value before.

In your case, if you want to start your counter from zero, you should initialise it with zero:

int number_placement = 0;

Before accessing it (in your case, incrementing it).

gbuzogany
  • 1,911
  • 16
  • 17