Like Tree1Likes
  • 1 Post By AndrewHunter

classes and outputting to a file

This is a discussion on classes and outputting to a file within the C++ Programming forums, part of the General Programming Boards category; hey I am trying to write a program that converts decimal to hex, binary etc. I then need to take ...

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

    classes and outputting to a file

    hey I am trying to write a program that converts decimal to hex, binary etc. I then need to take the info and output to a file. I am using a class and have the couts in my class functions I was wondering if there was a way I could write to the file from the class functions.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    void binary(int num);
    void hex(int num);
    void bcd(int num);
    int remove();
    
    class converter
    {
        private:
        #define MAXSIZE 12
        int bin[MAXSIZE];
        int sec[4];
        int remainder;
        int newValue;
        int k;
    
        public:
        converter();
        void binary(int num);
        void hex(int num);
        void bcd(int num);
        int remove();
    
    
    };
    
    converter::converter()
    {
        remainder = 0;
        newValue = 0;
        k = 0;
    }
    
    
    void converter::binary(int num)//converts to binary from decimal
    {
         ofstream myfile;
         newValue = num;
    
       while(newValue >= 1)
        {
            remainder = newValue%2;
    
           if (remainder == 0)
            {
                bin[k] = 0;
                k++;
            }
    
    
            else
            {
                bin[k]= 1;
                k++;
            }
            newValue = newValue/2;
        }
    
    
        for(k; k<8; k++)
        {
            bin[k]=0;
        }
    
         for(k = 7; k>=0; k--)
        {
    
            myfile << bin[k] << endl;
        }
    
    }
    
    void converter::hex(int num)
    {
        ofstream myfile;
        k = 0;
        int temp = 0;
        newValue = num;
    
      while(newValue >= 1 )
        {
            remainder = newValue%16;
    
                bin[k] = remainder;//used to add the remainder into the array
                k++;
    
    
            newValue = newValue/16;
    
        }
         for(k; k < 8; k++)//fills up the array to follow the format
            {
                bin[k] = 0;
            }
    
        for(int g = 1; g>=0; g--)
        {
            temp = bin[g];
            cout << setw (40);
    
            if(temp <= 9)//converts to the appropriate letter for hexadecimal.
            {
    
                myfile << temp;
            }
            else if(temp == 10)
            {
                    myfile << "A";
            }
            else if (temp == 11)
            {
                   myfile << "B";
            }
            else if (temp == 12)
            {
                   myfile << "C";
            }
            else if (temp == 13)
            {
                    myfile << "D";
            }
            else if (temp == 14)
            {
                   myfile << "E";
            }
            else
            {
                    myfile << "F";
            }
    
        }
    
    
    }
    
    void converter::bcd(int num)
    {
        int p = 0;
        int i = 0;
        int k = 0;
        ofstream myfile;
    
        newValue = num;
    
    
            if(num <= 9)
            {
                bin[k] = 0;
                k++;
                bin[k] = 0;
                k++;
                bin[k] = num;
                k++;
    
            }
            else if(num <100 && num > 9)
                {
                         bin[k] = 0;
                         k++;
                         bin[k] = num/10;
                         p = num%10;
                         k++;
                         bin[k]=p/1;
                         k++;
                }
            else
                {
                        bin[k] = num/100;
                        p = num%100;
                        k++;
                        bin[k]=p/10;
                        p = p%10;
                        k++;
                        bin[k] = p;
                        k++;
                }
    
            k = 0;
            while(k !=3)
            {
                newValue = bin[k];
                k++;
    
                while(newValue >= 1)
                {
                    remainder = newValue%2;
    
                        if (remainder == 0) //sends the value into the correct if statement.
                        {
                            sec[i] = 0;
                        }
                        else
                        {
                            sec[i]= 1;
                        }
                     i++;
                     newValue = newValue/2;
                 }
    
                            for(i; i<4; i++)
                            {
                                sec[i]= 0;
                            }
                            cout << setw (60);
                            for (i = 3; i >= 0; i--)
                            {
                                myfile << sec[i];
                            }
    
                i = 0;
    
        }
    
    }
    
    int main()
    {
        int i = 0;
        ofstream myfile;
        myfile.open ("numbers.txt");
        converter a;
                cout<< setw (0);
                for(i; i < 256; i++)
                {
                myfile << i << endl;
                a.binary(i);//function call
                a.hex(i);
                a.bcd(i);
                }
    
    
    
    
        myfile.close();
    
        cin.get();
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You can just pass your ofstream object to whatever method in your class you want to write to a file. Something like:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    class myclass{
    	private:
    		std::string Name;
    		int age;
    	public:
    		myclass();
    		void writeFile(std::ofstream&);
    };
    myclass::myclass(){
    	Name="Test";
    	age=2;
    }
    void myclass::writeFile(std::ofstream& myFile){
    
    	myFile << Name <<" "<<age<<std::endl;
    }
    int main(void){
    
    	myclass test;
    	std::ofstream myfile("example.txt");
    
    	test.writeFile(myfile);
    
    	myfile.close();
    
    	return(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.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Would I have to write a print function to use this std:: method or could I keep my code and add the std:: in the appropriate places

  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
    Would I have to write a print function to use this std:: method or could I keep my code and add the std:: in the appropriate places
    Just pass the ofstream object to whatever method you want to use it in. For instance, just pass it to your bcd method like:
    Code:
    converter::bcd(int num, ofstream& myFile){
    ...
    myFile << num;
    and then when you use your method, simply:
    Code:
    ...
    a.bcd(i, myfile);
    ...
    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
    Thanks, I believe I got it now I just need to format it so that my numbers aren't running together. I appreciate the help.

  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
    Thanks, I believe I got it now I just need to format it so that my numbers aren't running together. I appreciate the help.
    No problem, I am not sure what your requirements are for this project, but did you look at the alternative bcd solution in this thread?
    kt1991 likes this.
    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
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    I haven't yet I will once I finish, and the requirements were literally print it like he showed us on the work sheet he didn't give us a single requirement so it is kind of a guess on our parts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. outputting to file?
    By TonyG in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 05:55 AM
  2. C++ Outputting into a PDF file
    By allen9190 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2010, 11:15 PM
  3. Dev-C++ outputting .exe file
    By Yuri in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2005, 12:14 PM
  4. outputting to another file
    By mcorn in forum C Programming
    Replies: 12
    Last Post: 08-11-2002, 11:33 PM
  5. outputting to a file
    By simhap in forum C++ Programming
    Replies: 7
    Last Post: 11-20-2001, 02:16 PM

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