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