binary to decimal

need help to complete the followin program

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2141 Views - Last Post: 27 April 2009 - 07:21 AM Rate Topic: -----

#1 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

binary to decimal

Post icon  Posted 24 April 2009 - 03:23 PM

#include <iostream>
#include <fstream>

using namespace std;

void PrintError(fstream&, char, bool&);
int Conversion(fstream&, char&, bool&);

int main()
{
fstream InFile;
char Bit;
int Decimal;
bool BadBit = false;

//Open file to read
InFile.open("f:\\Binary.txt",ios::in);

//Read the first bit of the number
InFile.get(Bit);

//continue reading all numbers till the end of file
while (!InFile.eof())
{
//call Conversion to convert binary to decimal
Decimal = Conversion(InFile, Bit, BadBit);

//check if there is error
if (BadBit)
{
//Print out error message
PrintError(InFile, Bit, BadBit);
}
else
//output the equivalent value of decimal
cout<<" = " <<Decimal<<endl;

//read the next number
InFile.get(Bit);
}

InFile.close();

return 0;
}

int Conversion(fstream& In, char& InBit, bool& Error)
/* Pre: The stream is open for input.
InBit is 1 or 0.
Error indicates the InBit is not One or Zero.
Post: return the conversion of binary to decimal.
Resets Error to true when encounter error.
*/
{

}


void PrintError(fstream& In, char BadOne, bool& Error)
/* Pre: The stream is open for input.
BadOne is bad digit.
Error indicates the BadOne is bad digit.
Post: Prints error message.
Resets Error to false means there is no error.
*/

{

}

Is This A Good Question/Topic? 0
  • +

Replies To: binary to decimal

#2 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Post icon  Posted 24 April 2009 - 04:33 PM

#include <iostream>
#include <fstream>

using namespace std;

void PrintError(fstream&, char, bool&);
int Conversion(fstream&, char&, bool&);

int main()
{
fstream InFile;
char Bit;
int Decimal;
bool BadBit = false;

//Open file to read
InFile.open("f:\\Binary.txt",ios::in);

//Read the first bit of the number
InFile.get(Bit);

//continue reading all numbers till the end of file
while (!InFile.eof())
{
//call Conversion to convert binary to decimal
Decimal = Conversion(InFile, Bit, BadBit);

//check if there is error
if (BadBit)
{
//Print out error message 
PrintError(InFile, Bit, BadBit);
}
else
//output the equivalent value of decimal
cout<<" = " <<Decimal<<endl;

//read the next number
InFile.get(Bit); 
}

InFile.close();

return 0;
}

int Conversion(fstream& In, char& InBit, bool& Error)
/* Pre: The stream is open for input.
InBit is 1 or 0.
Error indicates the InBit is not One or Zero.
Post: return the conversion of binary to decimal.
Resets Error to true when encounter error.
*/
{

}


void PrintError(fstream& In, char BadOne, bool& Error)
/* Pre: The stream is open for input.
BadOne is bad digit.
Error indicates the BadOne is bad digit.
Post: Prints error message.
Resets Error to false means there is no error.
*/

{

}




plss help me write d functions in the [int Conversion(fstream& In, char& InBit, bool& Error) and void PrintError(fstream& In, char BadOne, bool& Error)] using the pre and post conditions as specified
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: binary to decimal

Posted 24 April 2009 - 07:18 PM

To convert to binary you could do some kind of for loop.

double total = 0;
for(int i = 0; i < str.length(); ++i)
{
  total += (str[str.length() - (1+i)] - '0') * pow(2.0, static_cast<double>(i));
}



That is if your binary was a string. I am not sure if this code will work I didn't test it.

Don't forget <string> and <cmath>

This post has been edited by eker676: 24 April 2009 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#4 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 24 April 2009 - 07:40 PM

View Posteker676, on 24 Apr, 2009 - 06:18 PM, said:

To convert to binary you could do some kind of for loop.

double total = 0;
for(int i = 0; i < str.length(); ++i)
{
  total += (str[str.length() - (1+i)] - '0') * pow(2.0, static_cast<double>(i));
}



That is if your binary was a string. I am not sure if this code will work I didn't test it.

Don't forget <string> and <cmath>




can u pls edit in my program.....m tryin to learn C++ on my own so m finding it difficult to write the pre and post conditions.....thanks for ur post....
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: binary to decimal

Posted 24 April 2009 - 08:31 PM

We don't just do your homework for you. How is that learning?
Was This Post Helpful? 0
  • +
  • -

#6 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 24 April 2009 - 08:35 PM

View PostJackOfAllTrades, on 24 Apr, 2009 - 07:31 PM, said:

We don't just do your homework for you. How is that learning?



sorry.....but its not my homework and can u pls read d post above....i m just 19 and tryin to study on my own....m preparing n testin myself if i could do my major in it or not....
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: binary to decimal

Posted 24 April 2009 - 08:40 PM

View Postrahul89, on 24 Apr, 2009 - 07:35 PM, said:

m just 19 and tryin to study on my own


Write some code we can comment on.

If we just give you the answer you'll have no idea if it's a good major for you or not.

Try to write some code to do what needs to be done and the comments you get on it will tell you more about whether this is the 'thing' for you or not.

BTW - Please go back to the original posting and edit it
:code:
Was This Post Helpful? 0
  • +
  • -

#8 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 26 April 2009 - 02:45 PM

#include <iostream>
#include <fstream>

using namespace std;


void PrintError(fstream&, char, bool&);
int Conversion(fstream&, char&, bool&);

int main()
{
	fstream InFile;
	char Bit;
	int Decimal;
	int rem,pro,mult;
	bool BadBit = false;

	//Open file to read
	InFile.open("H:\\Binary.txt",ios::in);

	//Read the first bit of the number
	InFile.get(Bit);

	//continue reading all numbers till the end of file
	while (!InFile.eof())
	{
		//call Conversion to convert binary to decimal
		
		rem = Bit % 10;
		pro = rem * mult;
		Decimal = Decimal + pro;
		mult = mult * 2;
		Bit = Bit / 10;
	 while( Bit> 0) // while-loop for error handling
	{
	   Bit = Bit % 10;
	   if (Bit > 1 || Bit < 0)
	{
	  BadBit = false; // changes the value of the boolean expression
	  break; // breaks the loop
	 }
	 else
	 Bit = Bit / 10;
	 }
						
		//check if there is error
		if (BadBit)
		{
			cout<< "Invalid binary number!\n";// displays the error message
		}
		
		else
		{
			//output the equivalent value of decimal
			cout<<Bit<<" = " <<Decimal<<endl;
		}
		//read the next number
		InFile.get(Bit);
	}
	InFile.close();
	cin.get(); cin.get();
	return 0;
}

int Conversion(fstream& In, char& InBit, bool& Error)
/* Pre:  The stream is open for input.
		 InBit is 1 or 0.
		 Error indicates the InBit is not One or Zero.
   Post: return the conversion of binary to decimal.
		 Resets Error to true when encounter error.
*/
{
int n;
char Bit;
if ( n == 0 || n == 1)
{
	cout<<Bit;
}
else
{
	cout<<"\n\n ERROR! BINARY has only 1 and 0!\n";
	return 0;
}
}


void PrintError(fstream& In, char BadOne, bool& Error)
/* Pre:  The stream is open for input.
		 BadOne is bad digit.
		 Error indicates the BadOne is bad digit.
   Post: Prints error message.
		 Resets Error to false means there is no error.
*/

{
	
}




i tried doin this....if now u can show me wht to do n edit in my program itself...have been workin on it from last 3 days.....thank u
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: binary to decimal

Posted 26 April 2009 - 05:31 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: binary to decimal

Posted 26 April 2009 - 06:02 PM

Quote

can u pls edit in my program.....m tryin to learn C++ on my own so m finding it difficult to write the pre and post conditions.....thanks for ur post....

Quote

We don't just do your homework for you. How is that learning?


one of the wisest things i've seen all day :^:

#include<iostream>
using namespace std;

void getBinary();
int main(){

getBinary();

return 0;
}
void getBinary(){

while(0==0){
cout<<"I am sorry, we don't do your homework here for you...\nWe only help :):):):):):):):):):):):)\n";
}

return;
}



there's your code :D
Was This Post Helpful? 0
  • +
  • -

#11 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 26 April 2009 - 06:45 PM

View PostJackOfAllTrades, on 26 Apr, 2009 - 04:31 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

actually i m getting some funny faces and smilies= 0 in the output...so i wanted to whts wrong in my program......
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: binary to decimal

Posted 26 April 2009 - 06:50 PM

don't tell me you actually copied and pasted my code and compiled it expecting that was the answer to your question...
Was This Post Helpful? 0
  • +
  • -

#13 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 26 April 2009 - 06:52 PM

View Postcomputerfox, on 26 Apr, 2009 - 05:50 PM, said:

don't tell me you actually copied and pasted my code and compiled it expecting that was the answer to your question...

u kiddin me.......
Was This Post Helpful? 0
  • +
  • -

#14 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: binary to decimal

Posted 26 April 2009 - 06:54 PM

so you didn't right...
Was This Post Helpful? 0
  • +
  • -

#15 rahul89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 24-April 09

Re: binary to decimal

Posted 26 April 2009 - 07:07 PM

View Postcomputerfox, on 26 Apr, 2009 - 05:54 PM, said:

so you didn't right...

not at all man.....m really tryin to figure out whts wrong in my program....but no1 seems to be helpin me out
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2