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;
}

First thing: Your values function does not know what "infile" is because "infile" is not in scope of that function.
You need to either:
1) pass a pointer or a reference to the (in) file
2) make the file module or global scope
3) *** read the file in a different manner without calling the values() function. ***

I think your mistaken thines01, it does understand infile. If you run the program, it brings in the numbers 175 for v1 and 3 for v3. But the compute function is not working and I don't understand why. I am getting a incorrect output. Anything you can see within the compute function that is wrong within that.

Are you in the same course as monkeybut?

I believe the problem is that the line 45 should be within the for-loop.

I also posted a solution on the thread by monkeybut, but I wouldn't suggest submitting that for your assignment, the prof will be very suspicious if you do.

Are you in the same course as monkeybut?

I believe the problem is that the line 45 should be within the for-loop.

I also posted a solution on the thread by monkeybut, but I wouldn't suggest submitting that for your assignment, the prof will be very suspicious if you do.

Well, the things you are using to solve this problem are way more advanced than what he are at. Yes, I think we might have the same professor or problem, but your solution does not help either of us. Going primarily off what we have already posted, you should be trying to help us with complicated and more advanced solutions. I am already solved my problem. However, don't try to help and go over our heads. Sorry if that is coming off rude or unappreciative, cause that is not what I am at all. However, it is frustrating to have to try and decode(dumb down) your solutions to fit out level of experience. But thank you for trying. Please any help in future is still and very much so appreciated.

Here is the corrected and fixed code:

// Base Conversion Program

#include <iostream>
#include <cmath>    // use the pow funciton           
#include <iomanip>  // set width and decimal
#include <fstream> // use file input and output

using namespace std;

ofstream outfile;       
ifstream infile;

int values();           // prototypees
int check(int);
float compute(float, float);

int values()                // grab values from infile
{
    int a;
    infile >> a;
    
    return a;
}

int check(int a)                // check base for 2 - 9
{
    while ((a < 2) | (a > 9))
    {
        outfile << "Base not between 2 and 9.\n";
        cin >> a;
    }
    return 1;
}

float compute(float a, float b)  // calculation function
{
    float c;
    float d;
    float e;
    int count = 1;
    
    while (a > 0)   // loop for conversion of each number
    {
    
    c = floor((float) a / (float) b);  // calculate the new numerator 
    d = (int) a % (int) b;            // remainder
    a = c;                            // REAASIGN c for loop
    e = e + (d * count);                // place holder for current conversion
    count = count * 10;                // moves the place holder
    
    }
    
    return e;                            // new base number
} 

int main()
{
    
    outfile.open("outfile.out");  // open outfile
    if (!outfile)                // check outfile
    {
        outfile << "Output file did not open.\n";
        return 1;
    }
    else
    {
        outfile << "Output file opened.\n";
    }

    infile.open("basefile.txt");  // open infile
    if (!infile)                    // check infile
    {
        outfile << "Input file did not open.\n\n";
        return 1;
    }
    else
    {
        outfile << "Input file opened.\n\n";
    }
    
    int v1 = 0;
    int v2 = 0;
    float v3 = 1;
    
    while (!infile.eof())
    {
        v1 = values();   // grab base 10 number 
    
        v2 = values();   // grab base number
    
        check(v2);        // runs check on base 2 - 9 
    
        v3 = compute(v1, v2);        // calculates new base number
    
        outfile << fixed << setprecision(0);    // sets no decimals and full output  
        outfile << "Your number " << v1 << " in base 10 is " << v3 << " in base " << v2 << ".\n\n";
    }

    infile.close();        // close infile
    outfile.close();        // close outfile
    
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.