std::map

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
#include "stdafx.h"
#include <map>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	map<int, string> astuntaine;
	astuntaine[0] = "000";
	astuntaine[1] = "001";
	astuntaine[2] = "010";
	astuntaine[3] = "011";
	astuntaine[4] = "100";
	astuntaine[5] = "101";
	astuntaine[6] = "110";
	astuntaine[7] = "111";

	string x = "54544588";
	istringstream stream(x);

	for (int i; stream >> i; ) {
		cout << astuntaine.at(i,x);
	}

	cout << "\n";
	return 0;
}


So i have this code and i want to go through x and print every single octal number as a binary number. Where am I making a mistake?
Last edited on
closed account (LA48b7Xj)
You could put spaces in the string and check the value of i is within 0 and 7 inside the loop with if statements.

1
2
3
4
5
6
string x = "5 4 5 4 4 5 8 8";
istringstream stream(x);

for (int i; stream >> i; )
    if(i >= 0 && i <= 7)
        cout << astuntaine[i];
Last edited on
The thing is the value of x will be a user input value so its not really possible..
closed account (LA48b7Xj)
It is very possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cout << "Enter num: ";

string x;
cin >> x;

stringstream ss;

for(auto e : x)
{
    int i;
    ss << e;
    ss >> i;
    ss.clear();

    if(i >= 0 && i <= 7)
        cout << astuntaine[i];
}
Last edited on
Yeah it does a job but it outputs only the first number in octal platform. Im really a beginner in this stuff so i cannot implement much of my knowledge to this.
closed account (LA48b7Xj)
Check my previous code, I forgot to clear the stringstream I think it works now.
Last edited on
It works perfect!!
Topic archived. No new replies allowed.