Thread: C++ program assistance needed

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    36

    Question C++ program assistance needed

    Hello all,

    I've got a question regarding a programming assignment that is due by midnight. The objective is to create a program that converts a binary number (up to 32 bits long) to decimal. The problems I have run up against thus far are:

    1) Without the error checking for an entry being 1 or 0, I have a problem getting the original binary number to be displayed on the "____ in binary is ____ in decimal." line.
    2) With error checking, it keeps automatically passing by the binary number entry line and saying that it is not a valid binary number.

    Here is my current code:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
      char entry[32];
      char decision, exit_option;
      int place = 0, digit, decimal = 0;
      int n = 0, power = 1;
    
      cout << "This program converts positive integer binary numbers"
           << " (up to 32 bits) into a decimal number.\n\n"
           << "Would you like to convert a binary number? (Y or y): ";
      cin >> decision;
      exit_option = 'Y';
    
      while((exit_option == 'Y')||(exit_option == 'y'))
      {
        if((decision == 'Y')||(decision == 'y'))
          {
          cout << "\n\nPlease enter your binary number:\n\n";
          cin >> entry[0];
          while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            }
          if((entry[place] != 1)||(entry[place] != 0))
            {
            cout << "\n\nThis is not a binary number. Try again? ";
            cin >> exit_option;
            }
          else
            {
            for(digit = place-1; digit >= 0; digit --)
              {
              decimal += (entry[digit] - '0')*power;
              power *= 2;
              }
            cout << entry[place] << "in binary is " << decimal << " in decimal.\n\n";
            cout << "Continue? (Y or y): ";
            cin >> exit_option;
            }
          }
        else
          {
          cout << "Thanks anyway!";
          exit_option = 'n';
          }
      }
      cout << "\n\nThank you for using the Binary Number Convertor!\n\n";
      return 0;
    }
    Thanks for any help/advice given.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's a difference between 1 (the number 1) and '1' (the character '1'). Since you're reading in characters, you should probably consider that entry[place] might be '1' or '0' rather than 1 or 0.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I re-introduced the single quotes around the 0 and 1 and the same result holds. It's something in the order/placement of the if, skipws/noskipws, or something.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Does it choke before the \n, or on the \n itself? (Notice that you read in the new-line character and then run the error check, before the while loop can stop you.)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    After running the program, here's what happens:

    First time through, it allows me to enter a binary number. After hitting enter, it comes up with the "This is not a binary number." line. When asked to continue and I input yes (y), it jumps back and again goes through to the "This is not a binary number." line without allowing me to enter a number.

    Code:
    This program converts positive integer binary numbers (up to 32 bits) into a decimal number.
    
    Would you like to convert a binary number? (Y or y): y
    
    
    Please enter your binary number:
    
    101
    
    
    This is not a binary number. Try again? y
    
    
    Please enter your binary number:
    
    
    
    This is not a binary number. Try again? y
    
    
    Please enter your binary number:
    
    
    
    This is not a binary number. Try again? y
    
    
    Please enter your binary number:
    
    
    
    This is not a binary number. Try again? n
    
    
    Thank you for using the Binary Number Convertor!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The not-being-able-to-do-a-second-try is due to your not resetting place before re-reading. And I'm still guessing that nothing-being-a-binary-number is due to the mishandling of the \n.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Quote Originally Posted by tabstop View Post
    The not-being-able-to-do-a-second-try is due to your not resetting place before re-reading. And I'm still guessing that nothing-being-a-binary-number is due to the mishandling of the \n.
    I'm not quite sure what you're getting at here then with the \n bit - d'you mean the while statement involving \n? That's the only place where it's used as a character. Would I need to move the error check of the "if" into the while statement? I'm confused.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Velocity View Post
    I'm not quite sure what you're getting at here then with the \n bit - d'you mean the while statement involving \n? That's the only place where it's used as a character. Would I need to move the error check of the "if" into the while statement? I'm confused.
    Well, yes; at the moment the only character you ever check to see if it is a 1 or a 0 is the \n at the end, since the check only happens after you are done with the while loop.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    All right, but when I do that the program goes insane. I just don't know where/when this error check should occur. When I don't have it in the program AT ALL, the program compiles and runs without much problem:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
      char entry[32];
      char decision, exit_option;
      int place = 0, digit, decimal = 0;
      int n = 0, power = 1;
    
      cout << "This program converts positive integer binary numbers"
           << " (up to 32 bits) into a decimal number.\n\n"
           << "Would you like to convert a binary number? (Y or y): ";
      cin >> decision;
      exit_option = 'Y';
    
      while((exit_option == 'Y')||(exit_option == 'y'))
      {
        if((decision == 'Y')||(decision == 'y'))
          {
          cout << "\n\nPlease enter your binary number:\n\n";
          cin >> entry[0];
          while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            }
          for(digit = place-1; digit >= 0; digit --)
            {
            decimal += (entry[digit] - '0')*power;
            power *= 2;
            }
          cout << entry[place] << "in binary is " << decimal << " in decimal.\n\n";
          cout << "Continue? (Y or y): ";
          cin >> exit_option;
          }
        else
          {
          cout << "Thanks anyway!";
          exit_option = 'n';
          }
      }
      cout << "\n\nThank you for using the Binary Number Convertor!\n\n";
      return 0;
    }
    The problem with THIS code is that it does not display the binary number at the end "____ in binary is ____ in decimal."

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Never mind, it doesn't work without difficulty.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to print entry, not entry[place], as that will (still) only print the newline character and not the whole thing.

    And you still need to set place back to 0 before trying to read in a second number.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I scrapped the one without error check -- after trying a 32-bit number it decided to display it as being -1 in decimal. :-/

    This is beginning to frustrate... even after setting place back to 0 at the top of the while loop and taking out entry[place] (inserting entry instead, is that what you meant?) it does the same thing.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Velocity View Post
    I scrapped the one without error check -- after trying a 32-bit number it decided to display it as being -1 in decimal. :-/
    You say that as though that is incorrect. However, 11111111111111111111111111111111 is -1. (Or, more specifically, how -1 is represented in most computers these days.)
    Quote Originally Posted by Velocity View Post
    This is beginning to frustrate... even after setting place back to 0 at the top of the while loop and taking out entry[place] (inserting entry instead, is that what you meant?) it does the same thing.
    You'll have to show me what you mean.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Quote Originally Posted by tabstop View Post
    You say that as though that is incorrect. However, 11111111111111111111111111111111 is -1. (Or, more specifically, how -1 is represented in most computers these days.)
    ...I knew that.

    <_<
    >_>

    You'll have to show me what you mean.
    Where "entry[place]" was in the original code, I replaced with just "entry". I don't think that's right though.

    The second bit of code (the one without the error check) runs the first time correctly, then upon re-entrance into the loop it goes nuts again and displays the same number over again. Code is below...

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main()
    {
      char entry[32];
      char decision, exit_option;
      int place = 0, digit, decimal = 0;
      int n = 0, power = 1;
    
      cout << "This program converts positive integer binary numbers"
           << " (up to 32 bits) into a decimal number.\n\n"
           << "Would you like to convert a binary number? (Y or y): ";
      cin >> decision;
      exit_option = 'Y';
    
      while((exit_option == 'Y')||(exit_option == 'y'))
      {
        if((decision == 'Y')||(decision == 'y'))
          {
          place = 0;
          cout << "\n\nPlease enter your binary number:\n\n";
          cin >> entry[0];
          while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            }
          for(digit = place-1; digit >= 0; digit --)
            {
            decimal += (entry[digit] - '0')*power;
            power *= 2;
            }
          cout << entry << "in binary is " << decimal << " in decimal.\n\n";
          cout << "Continue? (Y or y): ";
          cin >> exit_option;
          }
        else
          {
          cout << "Thanks anyway!";
          exit_option = 'n';
          }
      }
      cout << "\n\nThank you for using the Binary Number Convertor!\n\n";
      return 0;
    }
    Last edited by Velocity; 10-05-2008 at 07:23 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    cin >> exit_option leaves a \n in the input buffer, which is cheerfully read into input[0] at the top of the next loop. Adding "cin.ignore()" directly after this works wonders. (Edit: I mean directly after cin>>exit_option.)

    Next, you will discover that power isn't reset, and that maybe you want to reset your input array too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed with program - urgent thanks!
    By lildevil in forum C Programming
    Replies: 1
    Last Post: 03-09-2008, 06:45 AM
  2. Replies: 15
    Last Post: 12-31-2007, 06:26 AM
  3. Need assistance with program
    By DJ_Mittens in forum C Programming
    Replies: 4
    Last Post: 04-19-2005, 08:16 PM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  5. Program Ideas Needed
    By pkananen in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 10:08 PM