Oct 8, 2016 at 6:42pm UTC
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 Oct 8, 2016 at 6:49pm UTC
Oct 8, 2016 at 7:26pm UTC
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 Oct 8, 2016 at 7:32pm UTC
Oct 8, 2016 at 7:32pm UTC
The thing is the value of x will be a user input value so its not really possible..
Oct 8, 2016 at 7:40pm UTC
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 Oct 8, 2016 at 8:32pm UTC
Oct 8, 2016 at 7:57pm UTC
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.
Oct 8, 2016 at 8:07pm UTC
Check my previous code, I forgot to clear the stringstream I think it works now.
Last edited on Oct 8, 2016 at 8:32pm UTC