»
 

Go Back   ResellerRatings Store Ratings > ResellerRatings Forums > Tech Support

Reply
 
LinkBack Thread Tools Display Modes
Old 07-12-2003, 08:53 AM   #1 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 1
holguie81 is on a distinguished road
please help

Write a program that reads characters representing binary (base-2) from a data file and translates them to decimal base (base-10) numbers the binary and decimal numbers should be out put in two columns with appropriate headings here is a sample of the out put:

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 the binary numbers one character at a time. As each character is read, the program multiplies the total decimal value by 2 and adds either 1 0r 0, depending on the input character. The program should check for bad data; if it encounters any thing except a 0 or a 1, it should out put the message "Bad digit on input."

I am to the point where my program can get the character one at time (left to right). I understand how to do the math but I just can't figure out how to assign the pow() since the program is reading the characters left to right.

This is what I have so far I hope I am on the right track.

#include <iostream>
#include <fstream> // For file I/O
#include <string> // For sting data type

using namespace std;

int main()
{

ifstream inFile; // Data file


// Promts the user for a file name.

string fileName; // File name
string binary;
char currentChar;
int newChar0;
int newChar1;
int loopCount;

cout << "Input file name: ";
cin >> fileName;
cout << "\n";

// Opens the File.

inFile.open(fileName.c_str()); // Changes variable into a C string and opens the file.
if (!inFile) // Checks to see if the file was opened.
{
cout << "*** Can't open input file ***\n\n"; // If file not opened
return 1;
}

//

cout << "\nBinary Number\tDecimal Equivalent\n";

inFile.get(currentChar);
newChar0 = 0;
newChar1 = 1;
loopCount = 0;
while (inFile)
{

if (currentChar < 48 || currentChar > 49)
{
cout << "bad digit input\n";
return 1;
}

if (currentChar == 48 || currentChar == 49)
{
loopCount++;
}

if (currentChar == 48)
{
newChar0 = currentChar;
}

if (currentChar == 49)
{
newChar1 = currentChar;
}

cout << currentChar;






inFile.get(currentChar);
}

cout << "\n" << loopCount << "\n";

return 0;
}

holguie81 is offline   Reply With Quote
Old 07-31-2003, 01:35 PM   #2 (permalink)
Registered User
 
Join Date: Jul 2003
Posts: 2
raghuraman is on a distinguished road
Since you have the entire string in each row, you can copy each line in a char buffer and then traverse from right to left to calculate the decimal value.
raghuraman is offline   Reply With Quote
Reply




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Most Active Discussions

Recent Discussions

All times are GMT -6. The time now is 05:06 AM.