Assignment
You are to write a program that will convert any base 10 number a to any base b. Input two values, the first in base 10 and the second value is the base that is to be converted into. Bases only between 2 and 9, and flag invalid input.
basefile.txt includes this:
175 3
I am not getting a output yet, because of several errors. Please any help would be greatly appreciated.
// Base Conversion Program
#include <iostream>
#include <cmath> // use the pow funciton
#include <iomanip> // set width and decimal
#include <fstream> // use file input and output
#include <math.h>
using namespace std;
ofstream outfile;
ifstream infile;
int values();
int check(int);
int compute(int, int);
int values()
{
int a;
infile >> a;
return a;
}
int check(int a)
{
while ((a < 2) | (a > 9))
{
cout << "Base not between 2 and 9.\nPleae enter another: \n";
outfile << "Base not between 2 and 9.\nPleae enter another: \n";
cin >> a;
}
return a;
}
int compute(int a, int b)
{
int k;
int c;
int d;
k = floor(log10((double) a) / log10((double) b)) + 1;
c = pow((double) b, (double) k - 1);
for(int i = 0; i < k;)
{
cout << (a / c) << endl;
a = a % c;
k = k - 1;
}
return k;
}
int main()
{
outfile.open("outfile.out"); // open outfile
if (!outfile) // check outfile
{
cout << "Output file did not open.\n";
outfile << "Output file did not open.\n";
return 1;
}
else
{
cout << "Output file opened.\n";
outfile << "Output file opened.\n";
}
infile.open("basefile.txt"); // open infile
if (!infile) // check infile
{
cout << "Input file did not open.\n";
outfile << "Input file did not open.\n";
return 1;
}
else
{
cout << "Input file opened.\n";
outfile << "Input file opened.\n";
}
int v1 = 0;
int v2 = 0;
int v3 = 1;
v1 = values();
v2 = values();
cout << v1 << endl << v2 << endl;
v2 = check(v2);
v3 = compute(v1, v2);
outfile << "Your number " << v1 << " in base 10 is " << v3 << " in base " << v2 << ".\n";
cout << "Your number " << v1 << " in base 10 is " << v3 << " in base " << v2 << ".\n";
system("PAUSE");
return 0;
}