# binary to decimal

• (2 Pages)
• 1
• 2

## 23 Replies - 3708 Views - Last Post: 27 April 2009 - 07:21 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=101479&amp;s=87efd30888d4d02d28441eeef61d23ee&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rahul89

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

# binary to decimal

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;

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

//check if there is error
{
//Print out error message
}
else
//output the equivalent value of decimal
cout<<" = " <<Decimal<<endl;

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.
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

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

## Re: binary to decimal

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;

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

//check if there is error
{
//Print out error message
}
else
//output the equivalent value of decimal
cout<<" = " <<Decimal<<endl;

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.
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

### #3 eker676

• Software Engineer

Reputation: 379
• 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

### #4 rahul89

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

## Re: binary to decimal

Posted 24 April 2009 - 07:40 PM

eker676, 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....

• Saucy!

Reputation: 6247
• Posts: 24,014
• 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?

### #6 rahul89

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

## Re: binary to decimal

Posted 24 April 2009 - 08:35 PM

JackOfAllTrades, 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....

### #7 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: binary to decimal

Posted 24 April 2009 - 08:40 PM

rahul89, 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

### #8 rahul89

Reputation: 0
• 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;

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
{
cout<< "Invalid binary number!\n";// displays the error message
}

else
{
//output the equivalent value of decimal
cout<<Bit<<" = " <<Decimal<<endl;
}
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.
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

• Saucy!

Reputation: 6247
• Posts: 24,014
• 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

### #10 computerfox

• straight vegetarian kid

Reputation: 50
• 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;
}

```

### #11 rahul89

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

## Re: binary to decimal

Posted 26 April 2009 - 06:45 PM

JackOfAllTrades, 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......

### #12 computerfox

• straight vegetarian kid

Reputation: 50
• 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...

### #13 rahul89

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

## Re: binary to decimal

Posted 26 April 2009 - 06:52 PM

computerfox, 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.......

### #14 computerfox

• straight vegetarian kid

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

## Re: binary to decimal

Posted 26 April 2009 - 06:54 PM

so you didn't right...

### #15 rahul89

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

## Re: binary to decimal

Posted 26 April 2009 - 07:07 PM

computerfox, 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

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }