code only works with 3 digits

This is a discussion on code only works with 3 digits within the C++ Programming forums, part of the General Programming Boards category; This code for finding the binary representation of a number works if I type a number of 3 digits but ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    18

    code only works with 3 digits

    This code for finding the binary representation of a number works if I type a number of 3 digits but if i type a number like 3427 , it doesn't ...
    I use Dev-++ , I have tried to debug and apparently the problem is on the line after while , perhaps the ze[u] .



    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     long x,z,n,y=0,u;
     long ze[y];
     
    cout<<"write an integer : ";
    cin>>x; 
        for(n=x;n>0;n/=2){
        z=n%2;  
         cout<<z<<',';ze[y]={z}; y++;                 
         }
        cout<<"  The last digit  is the first digit\n of the binary representation of "<<x<<endl<<endl;
        cout<<" Now , the right order : "<<endl;
        u=y-1;
        while (u>=0){
              cout<<ze[u]<<",";u--;
              }
         char zs;cin>>zs;
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Knock out that array declaration with a variable as the size.

    This works:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     long x,z,n,y,u;
     y = 0;
     long ze[100];
     
    cout<<"Write an integer : ";
    cin>>x; 
        for(n = x; n > 0; n /= 2){
         z = n % 2;  
         cout << z;
         ze[y]=z;
         y++;                 
         }
        cout<<"  The last digit  is the first digit\n of the binary representation of "<<x<<endl<<endl;
        cout<<" Now , the right order : "<<endl;
        u=y-1;
        while (u>=0){
              cout<<ze[u];
              u--;
              if ((u+1) % 4 == 0)
                 cout << " "; 
              }
              }
         char zs;
         cin>>zs;
    }
    You're going out of bounds on your index, the program is trying to access memory that doesn't belong to it, and windows is giving it a spanking.
    Last edited by SlyMaelstrom; 11-12-2005 at 04:19 AM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    Thanks , it is more pleasant like that .

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    As a critique on the output, I don't understand why you even bother showing it backwards, it's much nicer looking like this.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     long x,z,n,y,u;
     y = 0;
     long ze[100];
     
    cout << "Write an integer: ";
    cin >> x; 
    cin.ignore();
        for(n = x; n > 0; n /= 2){
         z = n % 2;
         ze[y] = z;
         y++;                
         }
        cout<<"Binary Representation: "<<endl;
        u = y - 1;
        while (u >= 0){
              cout << ze[u];
              u--;
              if ((u+1) % 4 == 0)
                 cout << " "; 
              }
         cin.get();
    }
    Sample Output:
    Code:
    Write an integer: 364365
    Binary Representation:
    101 1000 1111 0100 1101
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    18
    The simple reason was that i didnt know how to dispay it rightward on the screen .
    The spaces between every 4 digits also make the result easier to read .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 06:52 AM
  2. Code works in MS Visual C++, not in Dev-C++
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-13-2005, 12:15 PM
  3. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  4. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  5. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21