1,075,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by monkeybut which have been Voted Up

IT currently is set up for base 3, but should be able to make a couple of minor changes and convert to any base, thanks and hope it helps someone out.

#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

void convert_base(int v, int b);
ifstream inputdata;
ofstream outputdata;

int main(int argc, char *argv[]){
    //int a, b, c, d, e, f, i;
    int v;
    int base = 3;

    inputdata.open("Base_Conversion_File.dat");
    if(!inputdata){
        cout << "There was a problem opening the data file." << endl;
        return 1;
    }
    inputdata >> v;
    outputdata.open("Converted_data.out");
    if(!outputdata){
        cout << "There was a problem created the output file." << endl;
    }
  while (inputdata >> v) {
    outputdata << v << " converted to base " << base << " is ";
    convert_base( v, base );
    outputdata << "\n";
  }
    inputdata.close();
    outputdata.close();
    return 0;
}

void convert_base(int v, int b){
    int k, i;
    int max;

    k = floor(log10(v)/log10(b)) + 1;
    max = k;

    for(i = 0; i < max; i++){
    outputdata << (int)(v/ pow(b, k-1));

        v = v % (int)pow(b, k-1);
        k = k - 1;

    }
    outputdata << endl;
}
monkeybut
Newbie Poster
15 posts since Sep 2011
Reputation Points: 11
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.0626 seconds using 2.46MB