I would like to make my program grab an integer from the user (binary) and convert it to a normal number, I plan to do this manually but I need to know how to do something first:

If the user enters, for example, 10110101 I need to be able to go through each single digit. Let's say that is the variable "x". How would I get to know what x[4] is?

I'm probably not explaining this very well, but I tried my best, I just need to be ably to grab single digits from an entered integer.

Thanks,

Recommended Answers

All 7 Replies

The easiest way would probably be to take the input as a string and compare each character to '1' and '0'. This will also help you tell if the user entered something like "101234" or "16oz."

The easiest way would probably be to take the input as a string and compare each character to '1' and '0'. This will also help you tell if the user entered something like "101234" or "16oz."

How do I do that? I'm very new to c++.

How do I do that? I'm very new to c++.

I would assume it would be something like:

char x[] = "10110101";
if (x[4] == /* whatever here */) {
}

// convert number to to decimal here
// ...

Do you know what arrays are?
Do you know the if statement?
Do you know the for statement?
Post your code after reading this and we can help.

Do you know what arrays are?
Do you know the if statement?
Do you know the for
statement?
Post your code and we can help.

yes, yes, and yes.

ARRAY example: 
int x[8];


IF example: 
if(x >= y)
                         {
                         //do this
                         }


FOR example: 
for(int counter = 0; counter = 10; counter += 1)
                         {
                         cout << counter << "\n";
                         }

I would assume it would be something like:

char x[] = "10110101";
if (x[4] == /* whatever here */) {
}

// convert number to to decimal here
// ...

Thanks, that works beautifully.

but one more question, is there any way to figure out how many objects were entered?

for example: user enters 1001, how can I return 4?

I suppose a better way to ask would be: How do i find out how many characters are in a string?

edit: never mind i got it, ill use: string x = ("0"); and x.size();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.