help with making a class and passing variables

This is a discussion on help with making a class and passing variables within the C++ Programming forums, part of the General Programming Boards category; hello, I am working on a program that takes numbers 0-255 and converts them to binary and hexadecimal and bcd. ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    help with making a class and passing variables

    hello, I am working on a program that takes numbers 0-255 and converts them to binary and hexadecimal and bcd. I believe my algorithms and remove function are correct but when I call the function in int main() I get an error saying the program is not responding. Any help would be appreciated.

    Code:
    #include <iostream>
    using namespace std;
    
    class converter
    {
        private:
        int bin[12];
        int remainder;
        int newValue;
        int k;
    
        public:
        converter();
        void binary(int);
        void hex(int);
        void bcd();
        int remove();
    };
    
    converter::converter()
    {
        int remainder = 0;
        int newValue = 0;
        int k = 0;
    
    
    }
    
    
    void converter::binary(int num)//converts to binary from decimal
    {
         newValue = num;
    
         while(newValue >= 1)
        {
            remainder = newValue%2;
    
           if (remainder == 0) //sends the value into the correct if statement.
            {
                bin[k] = 0;
                k++;
            }
    
    
            else
            {
                bin[k]= 1;
                k++;
            }
            newValue = newValue/2;
        }
    
    }
    
    void converter::hex(int num)
    {
        remainder = num % 16;
    
        while(newValue >= 1 )
        {
            remainder = newValue%16;
    
                bin[k] = remainder;//used to add the remainder into the array
                k++;
    
    
            newValue = newValue/16;
    
        }
    
         for(int z = k; z < 12; z++)//fills up the array to follow the format
            {bin[k] = 0;}
    }
    
    void converter::bcd()
    {
    }
    
    int converter::remove()//print out the values and remove the vaules reading the array for the new values.
    {
    
    
    
           int temp = bin[k-1];
           k--;//brings k back to 0 allowing for more info to be added to the array
    
            if(temp <= 9)//converts to the appropriate letter for hexadecimal.
            {
                return temp;
            }
            else if(temp = 10)
            {
                    temp = 'A';
                    return temp;
            }
            else if (temp = 11)
            {
                    temp = 'B';
                    return temp;
            }
            else if (temp = 12)
            {
                   temp = 'C';
                    return temp;
            }
            else if (temp = 13)
            {
                    temp = 'D';
                    return temp;
            }
            else if (temp = 14)
            {
                    temp = 'E';
                    return temp;
            }
            else
            {
                    temp = 'F';
                    return temp;
            }
    
    
    
    }
    int main()
    {
        int i = 10;
        converter a,b,c;
    
            cout << i <<endl;
            a.binary(i);//function call
            cout << "do we make it past binary";
    
                 for(int o = 1; o>= 0; o--)
                {
                    cout<<a.remove();//attempt to print off the array values
                }
    
    
        cin.get();
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Look at your if statements:
    Code:
    else if(temp = 10)
    Note the difference between = (the assignment) and == (comparator).
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Thanks, do you see anything wrong with my binary function because when I call it in the int main is where I get an error and I can't tell if it is from me passing a variable wrong or what. That is why I wrote the cout << do we make it past binary.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by kt1991 View Post
    Thanks, do you see anything wrong with my binary function because when I call it in the int main is where I get an error and I can't tell if it is from me passing a variable wrong or what. That is why I wrote the cout << do we make it past binary.
    Yes, I do. Take a look at your constructor definition:

    Quote Originally Posted by kt1991 View Post

    Code:
    converter::converter()
    {
        int remainder = 0;
        int newValue = 0;
        int k = 0;
    }
    See how you provided a type for those variables? Well, in C++ that means those are new variables, different from those that you defined as your private data members. Drop the types from those variables, e.g. it should just be k=0.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    wow I feel dumb now, thank you so much. The sad thing is I have written like 3 or 4 class programs I guess that is what happens when you take a summer break away from c++

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by kt1991 View Post
    wow I feel dumb now, thank you so much. The sad thing is I have written like 3 or 4 class programs I guess that is what happens when you take a summer break away from c++
    Don't worry about that too much, we all make mistakes. The important thing is that you learn from them. That being said, the scope rules continue to be one of the hardest concepts to workthrough in C/C++ so don't feel too bad.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    You should learn how to use a debugger. It will serve you well. Such things are trivial to find with a debugger.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a (atom) class in a (struct) class makes big errors!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-11-2007, 08:18 PM
  2. Making stack class using vector class
    By spank in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2007, 04:50 AM
  3. Passing Variables
    By scootsonline in forum Windows Programming
    Replies: 2
    Last Post: 09-14-2006, 10:11 AM
  4. passing variables
    By 182 in forum C++ Programming
    Replies: 12
    Last Post: 02-15-2006, 02:52 PM
  5. passing variables
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2001, 03:41 PM

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