Hi , some help needed i have the following task: reads a sequence of positive numbers and prints out their binary numeral system representations.
im done with the change code with decimal to binary which is the following:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int dec, len;
string bin;
cout << "Please enter a decimal number:" << endl;
cin >> dec;
bin = "";
while (dec != 0)
{
if (dec % 2 == 0)
bin.insert(0, "0");
else
bin.insert(0, "1");
dec = dec / 2;
}
cout << "The equivalent binary number is: " << bin << endl << endl;
}
but still im troubled about something , and that is that my teacher asks for a sequense of number ( like in one line) SO i though of making cin>>dec in a loop but still it would be a single entry in a loop so i was thinik if anyone could modify my code so it would input and output like the following program (which inputs a sequance of strings and outputs them in reverse charecter order)
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char** argv)
{
const string delims(" \t,.;");
string line;
// for every line read successfully
while (getline(cin,line)) {
string::size_type begIdx, endIdx;
// search beginning of the first word
begIdx = line.find_first_not_of(delims);
// while beginning of a word found
while (begIdx != string::npos) {
// search end of the actual word
endIdx = line.find_first_of (delims, begIdx);
if (endIdx == string::npos) {
// end of word is end of line
endIdx = line.length();
}
// print characters in reverse order
for (int i=endIdx-1; i>=static_cast<int>(begIdx); --i) {
cout << line[i];
}
cout << ' ';
// search beginning of the next word
begIdx = line.find_first_not_of (delims, endIdx);
}
cout <<" - is the reverse"<< endl;
}