# Help

• 10-23-2002
tkems
Help
Ok, im kinda really new at this.
I am making a simple menu program that will convert binary to decimal and vice versa, and the same with hex.
I need to know the code that will ask:

if (num < "so many char.")

if ("the third number from the right" of num is equal to a one or not)

or any number in the string.

• 10-23-2002
kermi3
Homework
Please don't ask people to do all your work for you, See the announcement on Homework at the top of the forum to see what is acceptable ro PM. Basically people are happy to help, but they're not going to do it all for you. Show us what you've got, show your code (using code tags), or where you're confused and someone will be happy to help you I'm sure. Feel free to PM me with any questions.

This is also a very popular topic, I would try using the board search feature to find a solution.

Good Luck,

Kermi3
• 10-23-2002
tkems
I wasnt asking for someone to do all my work.
I just asked for 2 functions that could help me out.

Here is my code:

Code:

```#include <iostream.h> int main() {  int resp;  float num;    for(;;)  {   cout << "            Conversion Program" << endl;   cout << "            ==================" << endl;   cout << "    Option  Description" << endl;   cout << "    ------  ---------------------" << endl;   cout << "      0    Quit" << endl;   cout << "      1    Binary - Decimal" << endl;   cout << "      2    Decimal - Binary" << endl;   cout << "      3    Hexadecimal - Decimal" << endl;   cout << "      4    Decimal - Hexadecimal" << endl;   cout << "      5    Hexadecimal - Binary" << endl;   cout << "      6    Binary - Hexadecimal" << endl << endl;   cout << "      Please choose an option." << endl;     cin >> resp;     if (resp == 0)   break;     switch(resp)   {     case 1:       cout << "Binary - Decimal..." << endl;       cout << "Enter the number needing converted:"       cin << num;       if (num```
That is were i am stuck.

Help
• 10-23-2002
kermi3
heh don't take offense from my post above, it's a post that I copy to all threads that are too vague etc. You were far more specific than most.

From what you are doing I would want to use a function so that you would have:

Code:

```switch(resp)   {     case 1:         BinaryToDecimal(/*whatever you need here*/);         break;```
Your other option is to put all the information and techniques that would be in that function inside the "case" mainly where you have started typing "if (num"

I hope this gives you a start on where you're stuck, if not, post again.
• 10-23-2002
tkems
still nothing
Im sorry but like i said, im kinda new at this. Im teaching my self out of a book.

I dont understand the "BinarytoDecimal." Is that a function or something?

I need to ask if the int "num" is less than 8 numbers long, thats gonna be my limit.

And i also need to know if like:

Someone input the number: 10010110, i need to ask if the first letter is a one, and then is the second letter a one, and so on...

• 10-23-2002
kermi3
I've got to run now, though 'm sure someone else will help. Try doing a serach of the boards, I know there have been posts like this before.

Good luck on teaching yourself, it's an adbrimable goal.

Kermi3
• 10-23-2002
Magos
Re: Help
Quote:

if (num < "so many char.")
Is it a string? Then use strlen(), which returns the number of characters in it, NOT including the NULL terminator.

Quote:

if ("the third number from the right" of num is equal to a one or not)
Division and Modulus are your friends :).
How do you represent the numbers? As an integer? As a string? In binary for the binary numbers?

To get the specific digit out of a decimal number, divide it with 10 until the number you want stands last:

12345
1234
123

(Since they are integers, they get truncated automatically)

Then use modulus 10 on that number. Basically, this picks out the last digit of a decimal number.

123 % 10 = 3

There, the third last digit from the original number!
• 10-28-2002
tkems
Thanks
Thanks you all so much, you have been a big help.
• 10-28-2002
tkems
one more thing
Hey, One more thing.

How do i use strlen() in my prog?

like this?

Code:

`strlen(num)`
If so what does that return?

Can i use it like this?

Code:

`if (strlen(num) < 8)`
Thanks
• 10-28-2002
Magos
strlen() returns the number of characters in a string (char array).

ie:

strlen("Hi") --> 2
strlen("Hello") --> 5
strlen("") --> 0
• 10-28-2002
tkems
so i cant use it like this?

if (strlen(num) > 8)

If "num" is a string then can i do that?
• 10-28-2002
tkems
This is as far as ive gotten i need help at the bottom.

Code:

``` #include <iostream.h> int main() {  int resp, x;  float num;    for(;;)  x = 0  {   cout << "            Conversion Program" << endl;   cout << "            ==================" << endl;   cout << "    Option  Description" << endl;   cout << "    ------  ---------------------" << endl;   cout << "      0    Quit" << endl;   cout << "      1    Binary - Decimal" << endl;   cout << "      2    Decimal - Binary" << endl;   cout << "      3    Hexadecimal - Decimal" << endl;   cout << "      4    Decimal - Hexadecimal" << endl;   cout << "      5    Hexadecimal - Binary" << endl;   cout << "      6    Binary - Hexadecimal" << endl << endl;   cout << "      Please choose an option." << endl;     cin >> resp;     if (resp == 0)   break;     switch(resp)   {     case 1:       cout << "Binary - Decimal..." << endl;       cout << "Enter the number needing converted:"       cin << num;       if (strlen(num) > 8)         {         cout << "Has to be under 8 numbers long!" << endl;         break;         }               else         num % 10         if ("what do i put here that has to do with the 'num % 10'" = 1)           {           x = x + 1           }               num = num / 10               num % 10         if ("what do i put here that has to do with the 'num % 10'" = 1)           {           x = x + 2           }                 num = num / 10               num % 10         if ("what do i put here that has to do with the 'num % 10'" = 1)           {           x = x + 4```
Am i doing things right? If not could you give some suggestions?

Thx
• 10-28-2002
Magos
Seems like you store the binary number as a variable, not a string.
Quote:

if (strlen(num) > 8)
Make that:
Code:

```float Val = (Num / 10000000.0); if(Val >= 1.0)```
You could also use bitshifting to convert Dec -> Bin
Code:

```  unsigned int Var = 12345;   unsigned int BitMask = (1 << ((sizeof(unsigned int) * 8) - 1));   for(int i=0; i<(sizeof(unsigned int) * 8); i++)   {       if(Var & BitMask) cout << "1";       else cout << "0";       BitMask >>= 1;   }```