Google Answers Logo
View Question
 
Q: C++ Program that converts binary numbers to decimals ( No Answer,   2 Comments )
Question  
Subject: C++ Program that converts binary numbers to decimals
Category: Computers > Programming
Asked by: lrod-ga
List Price: $15.00
Posted: 09 Nov 2002 23:48 PST
Expires: 22 Nov 2002 14:38 PST
Question ID: 104477
I need help writing a C++ program that reads characters representing
binary (base-2) numbers from a data file ( data.txt ) and translates
them to decimal (base-10) numbers. I want the binary and decimal
numbers to be output to the screen as well as to a file results.txt.

data.txt
	    101011B
1000B
     		  1011100010B
		  10002100B
101B
  111100001111B


I want the program to read binary numbers one character at a time and
multiply the total decimal value (which begins at 0) by 2 and and adds
either 1 or 0 depending upon the input character. The program should
check for bad data (anything except a 0 or 1). It should output the
message "Bad digit on input" and proceed to the next binary number.

The function ,int binToDec(string &binary), should read in a binary
number (from a  single line in the file) and return its decimal
equivalent.

I don't need the whole program written out, I would just like some
help with the logistics of the program.

Request for Question Clarification by rbnn-ga on 10 Nov 2002 00:39 PST
A few questions:

I cannot figure out from you sample output, if that is what it is,
what you would like the sample output to be. Why are non-binary
numbers, for example, suffixed with "B"? What is the significance of
the free-verse indentation?

What compiler are you using?

What is the range of "numbers" you would like supported to be? What
happens if there is a 10000 digit binary number in the file.

What do you mean by 

"The function ,int binToDec(string &binary), should read in a binary
number (from a  single line in the file) and return its decimal
equivalent."

I assume by "function ," you mean "function, ". But this function
seems to me to take a single string as input and return an int as
output, from its name and signature, yet your description suggestions
the function should do file-i/o and that the return value is an  "int"
whatever that means on your platform.

Since earlier you state that you want an error signalled if any
character other than 1 or 0 occurs in the data file, negative numbers
are not representatable, so why is your conversion function returning
int and not unsigned int?

Can the file have more than one number in it, and if so, what are the
different numbers separated by?

Clarification of Question by lrod-ga on 10 Nov 2002 15:36 PST
Here is a bit more clarification:

The binary and decimal numbers should be output to the screen as well
as to a file, results.txt, in two columns with appropriate headings.
Here is a sample output file:

Binary Number        Decimal Equivalent 
1                            1 
10                           2 
11                           3 
10000                        16 
10101                        21 

There is only one binary number per input line, but an arbitrary
number of blanks can precede the number. The program must read binary
numbers one character at a time. As each character is read, the
program should multiply the total decimal value (which begins at 0) by
2 and and adds either 1 or 0 depending upon the input character. The
program should check for bad data; if it encounters anything except a
0 or 1, it should output the message "Bad digit on input" and proceed
to the next binary number.

I want to use a  function to convert Binary to Decimal. The function
should read in a binary number (from a  single line in the file) and
return its decimal equivalent. In addition, the function should accept
a reference parameter of type string, which is modified to reflect the
binary number read in. The function prototype should be:

int binToDec(string &binary); 

For example, when the fuction is called the very first time, it
returns the integer 43 and the reference parameter binary contains 
101011 (see the file data.txt)

data.txt 

	    101011B
1000B
     		  1011100010B
		  10002100B
101B
  111100001111B




I will be using an Xwin32 compiler.  If you you need more
clarification please feel free to ask.

Clarification of Question by lrod-ga on 10 Nov 2002 15:39 PST
I would like for the program to consist of a nested while loop in the
main BinToDec function.
Answer  
There is no answer at this time.

Comments  
Subject: Re: C++ Program that converts binary numbers to decimals
From: tox-ga on 09 Nov 2002 23:59 PST
 
lrod-ga,
If you mean pseodocode then I can help you.
All you basically need to do is the following:

open (HANDLE, data.txt)
while (data.txt is not finished reading) {
var1 = a line of datat.txt
sum = 0
for (i=(lenght of var1)-1; i>=1; i--) {
 if (character i is not a 1 or 0) {
  sum = "Bad digit on input"
  exit loop
 } else {
  sum += character i of var1 * 2**(i-1)    //i-1 because first digit is 2**0
 }
}
  open (output.txt for appending)
  print the sum to output.txt
  close output.txt
}

If that's what you're looking for, tell me so I can post as a real answer.

-Tox-ga
Subject: Re: C++ Program that converts binary numbers to decimals
From: rac-ga on 10 Nov 2002 04:30 PST
 
Hi,
 Refer the following sites to get an idea.

http://codecentral.borland.com/codecentral/ccweb.exe/prodcat?prodid=8&catid=1
http://www.cs.uiowa.edu/~jones/bcd/decimal.html
http://www.filelibrary.com/Contents/DOS/75/1.html
http://www.engin.umd.umich.edu/CIS/course.des/cis400/cpp/binary.html

Hope it helps,
RAC

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy