Help

This is a discussion on Help within the C++ Programming forums, part of the General Programming Boards category; Ok, im kinda really new at this. I am making a simple menu program that will convert binary to decimal ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    29

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

    and i need to ask

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

    or any number in the string.

    Please help!!
    Talitore - Using DEV-C++

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    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
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    29
    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
    Talitore - Using DEV-C++

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    29

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

    Thanks for your help.
    Talitore - Using DEV-C++

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    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
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Help

    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.


    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!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    29

    Thanks

    Thanks you all so much, you have been a big help.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    29

    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
    Talitore - Using DEV-C++

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    strlen() returns the number of characters in a string (char array).

    ie:

    strlen("Hi") --> 2
    strlen("Hello") --> 5
    strlen("") --> 0

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    29
    so i cant use it like this?

    if (strlen(num) > 8)

    If "num" is a string then can i do that?
    Talitore - Using DEV-C++

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    29
    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
    Talitore - Using DEV-C++

  13. #13
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Seems like you store the binary number as a variable, not a string.
    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;
       }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21