Thread: Binary to Dec Source Code

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Binary to Dec Source Code

    Code:
    #include <iostream>
    #include <fstream>
    
    #include <conio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    class binAndDec
    {
    	public:
    
    		int loadFromFile(char File[10]);
    		void binToDec();
    
    		char textString[6];
    		char Copy[6];
    		int n;
    };
    
    int binAndDec::loadFromFile(char File[10])
    {
    	char text;
    
    	ifstream tFile(File);
    	tFile.get(textString);
    	tFile.close();
    
    
    
    	if(textString==0)
    	{
    		cout << "NULL File!" << endl;
    		return 1;
    	}
    	return 0;
    }
    
    void binAndDec::binToDec()
    {
    	for(int tmp=0;tmp<6;tmp++)
    	{
    		Copy[tmp]=textString[tmp];
    	}
    	n=0;
    
        for (int x=0; Copy[x] != '\0' && (Copy[x] == '1' || Copy[x] == '0'); x++)
        {
            n <<= 1;  
            if (Copy[x]== '1')
    		{
                n += 1;
    		}
        }
    }
    
    
    int main()
    {
    	binAndDec mains;
    	if(mains.loadFromFile("text.txt")==1)
    	{
    		return 1;
    	}
    	else
    	{
    		mains.binToDec();
    		cout << (char)mains.n << endl;
    	}
    	getch();
    	return 0;
    }
    My question is what's wrong when I use the get function? I'm going to set it up to load more than one string of binary. Any ideas?

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    My question is what's wrong when I use the get function?
    Why don't you tell us? What are the errors?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    sry...

    sry, I was a little tired last night.

    Code:
    D:\Programs\Projects\hex\main.cpp(27) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::get(char &)' : cannot convert parameter 1 from 'char [6]' to 'ch
    ar &'
    that's pointing at the use of get.

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You cannot use get() to read a string of characters, only single characters.
    Use getline() or the >> operator to read a string
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    well, getline seeems to be undefined... what library do I need for that... I thought it was stdio.h but it still doesn't work then.

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    What do I use to include getline?
    Is there anything else I can use if that isn't in my library?

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    getline() is included in iostream.h. So i can't understand the problem you are having.
    Be a leader and not a follower.

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    getline( ) is actually in iostream. Not iostream.h.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I get an unideclared identifyer for getline. Anyone wanna show me how it's used?

  10. #10
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    sounds like you are passing a variable name that hasn't been declared previously in your code. And xsquared: i know the declaration of getline is within the iostream.h file, and not the actual object code its self.
    Be a leader and not a follower.

  11. #11
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    no, it's saying that getline is undeclared.

    [copy]
    D:\Programs\Projects\hex\main.cpp(26) : error C2065: 'getline' : undeclared identifier
    [/copy]

    Isn't that what this means?

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Actually, what I meant is that you should never use <iostream.h>, just <iostream>.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    can you post the code so i can have a quick look at it?
    Be a leader and not a follower.

  14. #14
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    Code:
    #include <iostream>
    #include <fstream>
    
    #include <conio.h>
    #include <stdlib.h>
    
    using namespace std;
    
    class binAndDec
    {
    	public:
    
    		int loadFromFile(char File[10]);
    		void binToDec();
    
    		char textString[6];
    		char Copy[6];
    		int n;
    };
    
    int binAndDec::loadFromFile(char File[10])
    {
    	char text;
    
    	ifstream tFile(File);
    	tFile >> text;
    	getline(text);
                        //I've also tried just doing a direct getline(tFile)
    	tFile.close();
    
    
    
    	if(textString==0)
    	{
    		cout << "NULL File!" << endl;
    		return 1;
    	}
    	return 0;
    }
    
    void binAndDec::binToDec()
    {
    	for(int tmp=0;tmp<6;tmp++)
    	{
    		Copy[tmp]=textString[tmp];
    	}
    	n=0;
    
        for (int x=0; Copy[x] != '\0' && (Copy[x] == '1' || Copy[x] == '0'); x++)
        {
            n <<= 1;  
            if (Copy[x]== '1')
    		{
                n += 1;
    		}
        }
    }
    
    
    int main()
    {
    	binAndDec mains;
    	if(mains.loadFromFile("text.txt")==1)
    	{
    		return 1;
    	}
    	else
    	{
    		mains.binToDec();
    		cout << (char)mains.n << endl;
    	}
    	getch();
    	return 0;
    }

  15. #15
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    you have to pass in the number of bytes you want getline to read. Additionally, you also need to call the method from the ifstream object, so it knows to read from the file stream associated with the file and not the stdin filestream.

    so try,

    infile.getline(data, 80);
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. S-Extract, Self Extractor w/ Source Code
    By CrazyNorman in forum C++ Programming
    Replies: 3
    Last Post: 09-03-2006, 11:50 AM
  2. Source code....
    By Darkcoder in forum Game Programming
    Replies: 8
    Last Post: 03-07-2005, 08:58 PM
  3. how do you code in binary...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 01-27-2003, 05:14 PM
  4. where can I find source code for a binary tree?
    By binary_man in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2003, 09:53 AM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM