Thread: C++ program assistance needed

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    All right, good to know - now the problem is that when it displays the binary-decimal line, it still displays the \n line so that the binary number is one line above the rest... e.g.:

    Please enter your binary number:

    101
    101
    in binary is 5 in decimal.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You know where the \n is in your array -- it's at array[place]. So set array[place] to \0 and be done with it.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Okay thanks. Now the only thing left, I think, is putting in the error check for it being 1 or 0. Now, we already said that it has to be implemented into the while loop... but the last time I did that, the program went insane.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you can't really get characters one at a time anyway, the idea of checking things as they are read in is not perhaps as real-time as you were hoping. But anyway, you should put it in there, and if it fails set a flag. Then outside the loop either print the sorry message, or do the conversion, depending on the flag.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Okay, so I would have to do something like this?

    Code:
    ...
    
    while((exit_option == 'Y')||(exit_option == 'y'))
      {
        if((decision == 'Y')||(decision == 'y'))
          {
          place = 0;
          power = 1;
          cout << "\n\nPlease enter your binary number:\n\n";
          cin >> entry[0];
          while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            [[ERROR CHECK HERE]]
            }                                               
          for(digit = place-1; digit >= 0; digit --)
            {
            decimal += (entry[digit] - '0')*power;
            power *= 2;
            }
          entry[place] = '\0';
          cout << "\n\n" << entry << " in binary is " << decimal << " in decimal.\n\n";
          cout << "Continue? (Y or y): ";
          cin >> exit_option;
          cin.ignore();
          }
        else
          { 
          cout << "Thanks anyway!";
          exit_option = 'n';
          }
      }
    
    ...

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The check there, yes. And then you wouldn't do the conversion if the check didn't succeed.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I know that would involve a break somewhere in the loop, but I'm not sure how to go from there...?

    Code:
    ...
    
          while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            if((entry[place] != '1')||(entry[place] != '0'))
              break;
            }
          for(digit = place-1; digit >= 0; digit --)
            {
            decimal += (entry[digit] - '0')*power;
            power *= 2;
            }
          entry[place] = '\0';
          cout << "\n\n" << entry << " in binary is " << decimal << " in decimal.\n\n";
          cout << "Continue? (Y or y): ";
          cin >> exit_option;
          cin.ignore();
          }
    
    ...
    Last edited by Velocity; 10-05-2008 at 08:47 PM.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Breaking the loop would be A Very Bad Thing Indeed, since the rest of the input would not be processed and would interfere with future execution. Just set a flag, and if the flag is set when you get out don't do the conversion.

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    I'm not quite sure what you mean by a flag in this sense. I've got the if expression there, and I'm assuming I'll have to use "else" to get the conversion actually done (enclosing the "for" section inside an "else"). So with the "if" being "if it's neither 1 nor 0," then it's there I have to input the "try again?" question, right?

  10. #25
    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 mean by a flag in this sense. I've got the if expression there, and I'm assuming I'll have to use "else" to get the conversion actually done (enclosing the "for" section inside an "else"). So with the "if" being "if it's neither 1 nor 0," then it's there I have to input the "try again?" question, right?
    Absolutely not. You must process (or at least .ignore) all the input that was typed in -- otherwise when you ask if they want to go again, your program will say the answer is "100101". And you can't do the for until after the while loop is finished anyway; so you can't ask the question until you have all the input in the first place.

  11. #26
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Okay, what you're saying makes sense. My only problem now is just where does the "if" statement actually go. You said it has to go within the while loop, but now it's to where I have to let all the data be input. You mention cin.ignore() again... here's a snippet, am I on the right track or is what you're saying going completely over my head?

    Code:
    ...
           while (entry[place] != '\n')
            {
            place ++;
            cin >> noskipws >> entry[place];
            if((entry[place] != '1')||(entry[place] != '0'))
              {     
              cin.ignore();
              cout << "This is not a valid binary number. Try again? (Y or y) "
              cin >> exit_option;
              }
            }
           for(digit = place-1; digit >= 0; digit --)
            {
            decimal += (entry[digit] - '0')*power;
            power *= 2;
            }
    ...

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is what it should look like:
    Code:
          bool is_binary = true;
          cin >> entry[0];
          while (entry[place] != '\n')
            {
            if (entry[place] != '0' && entry[place] != '1') is_binary=false;
            place ++;
            cin >> noskipws >> entry[place];
            }          
          if (is_binary) {
            //convert
          } else {
            //print sorry
          }
          //print prompt

  13. #28
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Ohh, the check goes BEFORE the input...

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Velocity View Post
    Ohh, the check goes BEFORE the input...
    Well, you have to check the first one (input[0]) before you read the next one. Also, this prevents the "checking \n problem" we had before.

  15. #30
    Registered User
    Join Date
    Oct 2008
    Posts
    36
    Yeah that would make sense, wouldn't it... haha. Man, don't know how I missed that. Since it prevents the \n thing does this mean that the cin.ignore is unnecessary toward the end now or should I keep it in there for a safeguard?

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