Hello,

I'm working on a homework problem for class. I have a text tab delimited file and each row has a orginal base, target base, and a number that is represented in the orginal base. The program should read this file and be able to convert the number from the orginal base to the target base. I'm able to skip the header column and start reading the file into a sting vector, where each element is a line in the file, and that works fine. My problem is getting the actual numbers out of the vector in the orginal form they were in in the text file. For example, I have a convert.txt file that looks like this:

Or     Ta     N
B      B      0111011100
D      O      12345

So basically, have a problem converting the number part back into actual numbers. I'm grateful for any help! Here is my code below:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <array>
#include <sstream>

using namespace std;


int dectobin()
{
    return 0;
}

int dectooct()
{
return 0;
}

int bintodec()
{return 0;
}

int octtodec()
{return 0;
}

int bintooct()
{return 0;
}

int octtobin()
{return 0;
}




int main()
{
    string filename;
    ifstream file_id;
    string line;
    vector<string> data;

    /*string num;
    int n;
    vector<int>numbers;*/

    cout << "Input the source file and include the file extention: ";
    cin >> filename;

    file_id.open(filename, ios::in);

    if (!file_id)
    {
        cerr << "Invalid filename.";
        exit(1);

    }

    file_id.ignore(30, '\n');

    while (!file_id.eof())
    {
        getline(file_id, line);
        data.push_back(line);
    }

//Not complete, the if statements will call the correct converting function
    for (int i = 0; i< data.size(); i++)
        {
            char org = data[i][0];
            char tar = data[i][2];

            if (org == 'B'&& tar =='D')
                cout <<"B TO D" <<endl;
            if (org == 'B'&& tar =='O')
                cout <<"B TO O" <<endl;
            if (org == 'B'&& tar =='B')
                cout << "same" << endl;
            if (org == 'D'&& tar =='B')
                cout <<"D TO B" <<endl;
            if (org == 'D'&& tar =='O')
                cout <<"D TO O" <<endl;
            if (org == 'D'&& tar =='D')
                cout << "same" << endl;
            if (org == 'O'&& tar =='D')
                cout <<"O TO D" <<endl;
            if (org == 'O'&& tar =='B')
                cout <<"O TO B" <<endl;
            if (org == 'O'&& tar =='O')
                cout << "same" << endl;



        }

    /*
    I used this to make sure the vector was being filled
    for (int i = 0; i < data.size(); i++)
    {
        for (int j = 0; j < data[i].size(); j++)
            cout << "Printing index ["<<i<<"]["<<j<<"]: "<<data[i][j] << endl;
    }
    cout << endl; */



    /*
    this didn't quite work the way I wanted it to 
    n came back as a negative number, but I wanted to keep this bit in to 
    show what I came up with so far

    for (int i = 0; i < data.size(); i++)
    {
        for (int j = 4; j < data[i].size(); j++)
            if(isdigit(data[i][j]))
                {
                    std::stringstream ss(data[i][j]);
                    ss >> n;
                    cout << n <<endl;
                }

            numbers.push_back(n);





    }*/






    return 0;
}

Recommended Answers

All 2 Replies

Or better yet, is there a way I can turn part of a vector element to a string?

Using a class that holds the from character, the to character and the number, will simplify things quite a bit for you. A vector will then have all the info you need in each item:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <array>
#include <sstream>
using namespace std;
class ConvertLine
{
public: 
    char From, To;
    int Number;
    ConvertLine()
    {

    }
    ConvertLine(char _From, char _To, int _Number)
    {
        From = _From;
        To = _To;
        Number = _Number;
    }
};

int main()
{
    string filename;
    ifstream file_id;
    string line;
    vector<ConvertLine> data;
    /*string num;
    int n;
    vector<int>numbers;*/
    cout << "Input the source file and include the file extention: ";
    cin >> filename;
    file_id.open(filename, ios::in);
    if (!file_id)
    {
        cerr << "Invalid filename.";
        exit(1);
    }
    file_id.ignore(30, '\n');
    while (!file_id.eof())
    {
        ConvertLine input;
        file_id >> input.From;
        file_id.ignore(5,'\t');
        file_id >> input.To;
        file_id.ignore(5,'\t');
        file_id >> input.Number;
        file_id.ignore(20,'\n');
        data.push_back(input);
    }

This assumes that the textfile is formatted for display and uses tabs(\t), if not just adjust the character count in the ignore statements.

Stringstream will work to convert any of the numbers to string:

stringstream ss;
ss << data[0].Number;
string num = ss.str();
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.