thanks Write a program that reads in an input file with one

thanks!!!

Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to decimal, hexadecimal, and octal numbers. Your program should take file name as input from the user. The binary numbers must be read in as strings. It is up to you to choose the variable types needed otherwise. Additionally, your program should define at least one function (likely, you\'ll end up with at least 3). If your program does NOT have at least one function, you will not get credit for this part of the assignment, even if your program passes submits grading. You have to utilize the following devices in your program (required): For example, given a file called bin.txt, which contains the following: A session should then look like the following example, including whitespace (no whitespaces at the end of the lines) and formatting:

Solution

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int toDecimal( string );
string toHex( int );
string toOct( int );

int main( ){
   cout << \"Enter filename:\" << endl;
   string fname;
   cin >> fname;
   vector<string> binaries;
   ifstream in( fname.c_str() );
   string input;
   while( in >> input ){ binaries.push_back(input); }

   for(int i = 0; i < binaries.size(); i++){
       cout << \"The binary number \" << binaries[i] << \" equals \";
       int decVal = toDecimal(binaries[i]);
       cout << decVal << \" in decimal, \";
       cout << toHex( decVal ) << \" in hexadecimal, \";
       cout << \" and \" << toOct( decVal ) << \" in octal\" << endl;
   }

   return 0;
}

int toDecimal( string bn ){
   int result = 0;
   for(int i = 0; i < bn.size(); i++){
       result*= 2;
       result += (int)(bn[i]-\'0\');
   }
   return result;
}

string toHex( int num ){
   string result = \"\";
   while( num != 0 ){
       char rem = (char)(num%16 + \'0\');
       if( num%16 > 9 ){
           rem = (char)( (int)(\'A\') + (num%16 - 10) );
       }
       result = rem + result;
       num = num/16;

   }
   return result;
}

string toOct( int num){
   string result = \"\";
   while( num != 0 ){
       char rem = (char)(num%8 + \'0\');
       result = rem + result;
       num = num/8;
   }
   return result;
}

thanks!!! Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to deci
thanks!!! Write a program that reads in an input file with one or more binary numbers separated by spaces or newline characters and outputs a conversion to deci

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site