Search code examples
c++octal

c++ - why the result is minus?


I have some problem with my simple program where if my input more than 295600127, the result is minus (-).

here :

#‎include‬ <iostream>
#include <windows.h>
using namespace std;
int konarray(int b);
void konbil(int A[], int &n);
int kali(int x);
main(){
   int b;
   char *awal,akhir,pil;
   awal:
   system("COLOR 9F");
   cout<<"enter decimal\t= ";cin>>b;
   //calling function of conversion to an array of integers
   konarray(b);
   akhir:
   cout<<"\n\nDo You Want To Reply Now ? (Y/N) : ";
   cin >> pil;
   cout<<endl;
   switch(pil){
      case 'Y' :
      case 'y' :
         system ("cls");
         goto awal;
          break;
      case'N':
      case 'n' :
          break;
      default:
      system("COLOR c0");
      system ("cls");
      cout << "Please, make sure for entering the choise!!\n\n\n\n";
      goto akhir;
   }
}
//convertion numer to array
int konarray(int b){
   int A[30];
   int i,n,h,s;
   i=0;
   do{
      h=b/8;
      s=b%8;
      b=h;
      A[i]=s;
      i++;
   }
   while(h!=0);
   n=i;
   for(i=0;i<n;i++)
      cout<<A[i]<<" ";
   konbil(A,n);
}
//array to octal
void konbil(int A[],int &n){
   int c,i;
   c=A[0];
   for(i=1;i<n;i++)
      c=c+A[i]*kali(i);
   system("COLOR f0");
   cout<<endl<<"the results of the conversion are\t= ";
   cout<<c<<endl;
}
int kali(int x){
   if (x==1)
      return (10);
   else
      return(10*kali(x-1));
}

i have tried change of all int into long, but it was same.
I want to know some reason why?
and how to fix it?


Solution

  • Have you tried

    long long int
    

    This program

    int main(int argc, char** argv)
    {
        cout << sizeof(int) << endl;
        cout << sizeof(long int) << endl;
        cout << sizeof(long long int) << endl;
    
         return 0;
    }
    

    gives

    4
    4
    8
    

    showing you need long long int to get 64 bits

    Change like this:

    void konbil(int A[],int &n){
       unsigned long long c,i;     // unsigned long long 
       c=A[0];
       for(i=1;i<n;i++)
          c=c+A[i]*kali(i);
       system("COLOR f0");
       cout<<endl<<"the results of the conversion are\t= ";
       cout<<c<<endl;
    }
    

    The largest positive number you can store int an int is 2147483647 (2^31 - 1). Adding just 1 to that number will result in the value -2147483648 (- 2^31).

    So the answer is that you have an overflow while using int. Therefore you need long long int or even better unsigned long long.

    unsigned long long can't be negative and allows the maximum value (2^64 - 1).

    EDIT: An extra question was added in the comment - therefore this edit.

    int

    On most systems an int is 32 bits.

    It can take values from -2^31 to 2^31-1, i.e. from -2147483648 to 2147483647

    unsigned int

    An unsigned int is also 32 bits. However an unsigned int can not be negative.

    Instead it has the range 0 to 2^32-1, i.e. from 0 to 4294967295

    long long int

    When you use long long int you have 64 bits.

    So the valid range is -2^63 to 2^63-1, i.e. -9223372036854775808 to 9223372036854775807

    unsigned long long

    A unsigned long long is also 64 bits but can not be negative.

    The range is 0 to 2^64-1, i.e. 0 to 18446744073709551615

    Try this code:

    int main()
    {
        cout << endl << "Testing int:" << endl;
    
        int x = 2147483647;   // max positive value
        cout << x << endl;
    
        x = x + 1;            // OVERFLOW
        cout << x << endl;
    
        cout << endl << "Testing unsigned int:" << endl;
    
        unsigned int y = 4294967295;   // max positive value
        cout << y << endl;
    
        y = y + 1;            // OVERFLOW
        cout << y << endl;
    
        cout << endl << "Testing long long int:" << endl;
    
        long long int xl = 9223372036854775807LL;   // max positive value
        cout << xl << endl;
    
        xl = xl + 1;            // OVERFLOW
        cout << xl << endl;
    
        cout << endl << "Testing unsigned long long:" << endl;
    
        unsigned long long yl = 18446744073709551615ULL;   // max positive value
        cout << yl << endl;
    
        yl = yl + 1;            // OVERFLOW
        cout << yl << endl;
    
        return 0;
    }
    

    it will give you

    Testing int:
    2147483647
    -2147483648
    
    Testing unsigned int:
    4294967295
    0
    
    Testing long long int:
    9223372036854775807
    -9223372036854775808
    
    Testing unsigned long long:
    18446744073709551615
    0
    

    showing how you overflow from max positive value by adding just 1.

    Also see this link http://www.cplusplus.com/reference/climits/