0

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,

4
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by Matt Tacular
0

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."

0

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++.

0

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
// ...
0

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.

0

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";
                         }

Edited by happygeek: fixed formatting

0

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?

0

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();

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.