How can i program this thing to print in the screen all the elements of the array, without knowing how many elements are there ( i cant do this: cout<< rem[0]<<rem[1]...), and how can i reverse that order, so that the last element is the first one.
You can almost certainly determine how many elements are in the array. If the data entered by the user was read as a string, you can use strlen() on it, or the .length() member of C++ std::strings. It probably wasn't read as a number, but even if it was, you can do this, too.
I think perhaps you'd be interested in looking at this program.
Code:
#include <iostream>
#include <cstdlib>
#include <string>
int main() {
std::string data, rev;
while(std::getline(std::cin, data)) {
std::cout << "==== Processing number \"" << data << '"' << std::endl;
std::cout << "Printed forwards: \"";
for(int x = 0; x < data.length(); x ++) {
std::cout << data[x];
}
std::cout << '"' << std::endl;
// build a string, the reverse of data
rev = "";
for(int x = data.length() - 1; x >= 0; x --) {
rev += data[x];
}
std::cout << "Printed backwards: \"" << rev << '"' << std::endl;
std::cout << "Converted to decimal via strtol(): "
<< std::strtol(rev.c_str(), NULL, 2) << std::endl;
}
return 0;
}
Notes: strtol() is limited to the range of a long. If you want higher values, you'll have to do the addition yourself. It's not hard. Do it as you would do it on paper.
Your compiler may warn you about "comparison between signed and unsigned integer expressions". This is because I used ints, a signed type, and std::string::length() returns an unsigned type. I didn't use an unsigned type because it messes up "x >= 0", and I didn't want the example to be too confusing . . . .