-4
#include <iostream>
#include <math.h>
using namespace std;

class ip{
private:
    string ip;
    int result[8];
    int sum;
public: 
   void input(){
    /* cout<<"Enter First 8 Binary in Ip address: ";
     cin>>ip;
     for(int i=0,j=7;i<8 ,j>=0;++i,--j){
        if(ip[i]=='1'){
            result[i]=pow(2,j);
        }else if(ip[i]=='0'){
            result[i]=0;
        }
    }
     for(int i=1 ; i<8 ; ++i){
     sum=sum+result[i];
   } */
   cout<<sum<<"\n";

 }
};

int main() {
ip convert;
convert.input();

return 0;
}

I was getting some problem while running this code then I understood the problem is with integer initialization...

please help me as I'm getting unwanted output

after running this code my output is: 131 I expected '0' as output why is it so

  • Firstly, your code is commented out. Secondly, what were you trying to do by specfiying `i<8, j>=0` as condition in `for`? What do you think this condition means? – AnT stands with Russia Aug 05 '18 at 06:11
  • 1
    But there is no "integer initialization" in your (not commented) code. Please create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of the *failing* code to show us, and tell us *how* it fails. – Some programmer dude Aug 05 '18 at 06:12
  • And looking at your commented code, it seems you could use [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read (from the beginning). – Some programmer dude Aug 05 '18 at 06:14
  • actually, I was making Ip address converter from binary to integers values, I was getting the problem so that's why I commented out my code to check my variable sum initialization – Deep-Programming Aug 05 '18 at 06:36

2 Answers2

1

You're right, one problem is that you don't initialise sum to zero. Also int i = 1 should surely be int i = 0.

sum=0;
for(int i=0 ; i<8 ; ++i){
    sum=sum+result[i];

There are lots of other problems with the code including unnecessary use of a class, unnecessary use of class variables, unnecessary use of floating point functions, unnecessary use of temporary array etc. etc. This program could be much simpler.

john
  • 85,011
  • 4
  • 57
  • 81
1

In C++, Automatic storage duration integer variables are not initialized to any particular value, and will contain whatever garbage bit pattern that happens to already be in those memory locations. From the perspective of the language standard, the value of the variable is indeterminate, and using it leads to undefined behavior. If you had defined it as a static variable, it would be auto-initialized to 0 in C++.

Most likely your compiler will also throw a warning if you try to use a uninitialized variable.

In GCC, you will see the warning if you compile with below flag:
-Wuninitialized
Warn if an automatic variable is used without first being initialized or if a variable may be clobbered by a setjmp call. In C++, warn if a non-static reference or non-static const member appears in a class without constructors.

Update based on Peter's comment:
In the above case, the code creates an automatic storage duration object of that class type. However, since you just declare an object of type ip as ip convert and you don't have your own constructor which initializes class objects' member values, compiler's default constructor will be called. Most compilers (if not all) will not initialize member values for you and hence you see an output corresponding to the bit pattern present in the memory locations where the object got created.

Viral Modi
  • 1,957
  • 1
  • 9
  • 18
  • 1
    This is the right general direction, and the variables in question are not initialized. But they are not automatic storage duration variables; they are members of class, and the code creates an automatic storage duration object of that class type. The answer needs to go through the rules for initialization of member sub-objects by the compiler-defined default constructor. – Pete Becker Aug 05 '18 at 16:08
  • @PeteBecker: thanks, I updated the answer taking care of your comment – Viral Modi Aug 05 '18 at 17:43