HELP FOR A HOMEWORK ASSIGNMENT!!!

Here is the prompt,

To distinguish between the different base representations, typically the base of a number is attached as a subscript (if there is no subscript, assume the number is base 10). For example, the number 52809 in base 10 (i.e. 5280910) is represented as:
5280910 = 5*10000 + 2*1000 + 8*100 + 0*10 + 9*1 = 5*104 + 2*103 + 8*102 + 0*101 + 9*100
Base-2 numbers (aka binary numbers) can be represented similarly. For example, the number 11001 in base-2 (i.e. 110012) can be converted to base-10 via the following algorithm:
110012 = 1*24 + 1*23 + 0*22 + 0*21 + 1*20 = 16 + 8 + 0 + 0 + 1 = 2510
In this homework assignment, your program will read input from a file named "input.txt". Each line of the file will contain a number with its specified base separated by an underscore '_' (e.g., 10111_2 is 101112) and a new desired base. Note that the underscore indicates the next number is a subscript. For example, 10111_2 10 is read as 10111 in base-2 and will be converted to base 10. Here is an example file:
10111_2 10 62_10 2 391_10 2 67_2 10 11_10 2

if anyone can give me a helpful starting point that would be great thank you.
I have the code that converts base 2 into base 10. Stuck after that
lol goodluck shit is hard af
Assuming my test.txt file is:
10111_2 10
62_10 2
391_10 2
67_2 10
11_10 2

The approach would be:
1. read file line by line into a string
2. find and replace the underscore character in each line with white-space (why white-space? see 4 below) - so now you have a modified string
3. construct a stringstream object with the modified string
4. stringstream reads characters up to white spaces into variables, so now read off the stringstream object created from the modified string into 3 integers - the number itself, the old base and the new base
5. save these integers in a vector of tuples
6. range loop (or iteratw if you wish) across the vector, running the appropriate base conversion function for each vector element. Note, I have declared, but not defined, the base conversion functions. You mention having code for converting base 2 to base 10 and you can search for the base 10 to base 2 conversion function and add in any other required header files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<tuple>

using namespace std;

int conv10to2(int num);
int conv2to10(int num);

int main(){

ifstream File;
vector<tuple<int, int, int>> v;
int num, old_base, new_base;

File.open("F:\\test.txt");
if(File.is_open()){
        string line;
        while(getline(File, line)){
               replace(line.begin(), line.end(), '_', ' ');
            stringstream stream(line);
            while(stream>>num>>old_base>>new_base){
                v.emplace_back(num, old_base, new_base);
            }
         }
    }
    for (auto& itr:v){
        if(get<2>(itr) == 2){
            cout<<get<0>(itr)<<"base "<<get<1>(itr)<<"is "<<conv10to2(get<0>(itr))<<"base "<<get<2>(itr)<<endl;
        }
        else{
            cout<<get<0>(itr)<<"base "<<get<1>(itr)<<"is "<<conv2to10(get<0>(itr))<<"base "<<get<2>(itr)<<endl;
        }
    }
}


Edit: upon further reading there are some suggestions that the numbers should be saved as doubles, not integers, take a look





Last edited on
Send me a private message.
OP: get<0>(itr) etc doesn't seem to be working. I've got to run to do something else, so I'll give you an alternative format to use in the meantime that has been tested and works:

1
2
3

    for (int i = 0; i < v.size(); i++){
        cout<<get<0>(v[i])<< " "<<get<1>(v[i])<< " "<<get<2>(v[i])<<endl;


PS: this is not the most elegant solution in my opinion but should get you going
OP: I've double checked my first post on this topic and the code there is fine
Topic archived. No new replies allowed.