Ignoring input.

This is a discussion on Ignoring input. within the C++ Programming forums, part of the General Programming Boards category; I have been making simple programs to solidify my basic knowledge of C++. But while making this Binary converter I ...

  1. #1
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200

    Ignoring input.

    I have been making simple programs to solidify my basic knowledge of C++. But while making this Binary converter I came across a problem.

    I cant seem to figure out how to ignore input after 8 characters. I want to read in the first 8 characters the user inputs and discard the rest.

    If cin.ignore is an option I cant figure out how to do it.

    Code:
    #include <iostream>
    
    int BinToDec ( char *bits )
    {
    	
    	int result = 0;
    	int mask = 1;
    	
    	for ( int loop = 7; loop >= 0; loop -- ) {
    		
    		if ( bits [loop] != '0' && bits [loop] != '1' ) {
    			
    			std::cout << "Error: Invalid Input\n";
    			return -1;
    			
    		}
    		
    		else if ( bits [loop] == '1' ) result += mask;
    		
    		mask *= 2;
    		
    	}
    	
    	std::cout << "\nResults:\n";
    	std::cout << "Bin: " << bits << '\n';
    	std::cout << "Dec: " << result << '\n';
    	std::cout << "Ascii: " << static_cast <char> ( result ) << '\n';
    	
    	return 0;
    	
    }
    
    int main ()
    {
    	
    	char bit_array [8];
    	memset ( bit_array, 0, 8);
    	
    	std::cout << "You must enter 8 bits. Q to exit\n\n";
    	
    	while ( 1 ) {
    		
    		std::cout << "\nEnter a binary value: ";
    		std::cin  >> bit_array;
    		
    		if ( bit_array[0] == 'q' ) break;
    		
    		BinToDec ( bit_array );
    		
    	}
    	
    	std::cout << "\nGoodbye.\n";
    	
    	return 0;
    	
    }
    What is C++?

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    cin.get( &bit_array[0], 8 );

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>&bit_array[0]
    bit_array

    **EDIT**
    Also, consider
    cin.read(bit_array, 8);

    I'm not 100% sure that'll work properly on cin (don't see why not though), but it's what I do when I want to read (n) bytes from a file.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Mm.

    Tried both of those and neither one worked. It had very odd behavior. It would work different everytime i tried it. Sometimes it would completely ignore everything i put in. ??
    What is C++?

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Hi Vicious,
    did you consider using get() method?
    I would suggest something like this:

    Code:
    int main ()
    {
    	
    	char bit_array [9];
    	memset ( bit_array, 0, 9);
    	
    	std::cout << "You must enter 8 bits. Q to exit\n\n";
    	
    	while ( 1 ) {
    		
    		std::cout << "\nEnter a binary value: ";
    		
    		std::cin.get(bit_array,9);
    
    		if(std::cin.get() !='\n')
    		{
    			std::cin.clear();
    			while((std::cin.get() !='\n'));
    		}
    		
    		if ( bit_array[0] == 'q' ) break;
    		
    		BinToDec ( bit_array );	
    	}
    	std::cout << "\nGoodbye.\n";
    	return 0;
    }
    I didn't test all possibilities but think it's OK.
    Cheers!

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Vicious, what's your code (full listing preferred, if it's short enough)?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There are plenty of ways to read the eight (?) characters that you want. Discarding everything after that is equally varied. Two fairly common ones that don't use an explicit loop are:
    Code:
    #include <limits>
    
    cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
    and
    Code:
    cin.ignore ( cin.rdbuf()->in_avail() );
    A quick implementation is obvious once you have those:
    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    int bit_convert ( char bits[] )
    {
      int dec = 0;
      int base = 1;
    
      for ( int i = 7; i >= 0; i-- ) {
        if ( bits[i] != '0' )
          dec += base;
    
        base <<= 1;
      }
    
      return dec;
    }
    
    int main()
    {
      char bits[9];
    
      cout<<"Enter an octet: ";
      if ( cin.getline ( bits, sizeof bits ) ) {
        cin.ignore ( cin.rdbuf()->in_avail() );
    
        cout<<"The value of "<< bits <<" is "<< bit_convert ( bits ) <<endl;
      }
    }
    Slightly harder is handling errors and realizing that a byte isn't always eight bits, then changing your code to deal with that.
    My best code is written with the delete key.

  8. #8
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Quote Originally Posted by Hunter2
    Vicious, what's your code (full listing preferred, if it's short enough)?
    Heh thats the full code in the first post.


    And thank you Prelude. I do not believe I have ever used these methods this way.
    What is C++?

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Heh thats the full code in the first post.
    Oh, I thought maybe you updated it when playing around with get() and read().

    >>And thank you Prelude. I do not believe I have ever used these methods this way.
    Ditto.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Prelude
    Slightly harder is handling errors and realizing that a byte isn't always eight bits, then changing your code to deal with that.
    Can you give some examples where byte isn't always eight bits?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by Micko
    Can you give some examples where byte isn't always eight bits?
    Do a search...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you give some examples where byte isn't always eight bits?
    The only machines I know of are old (like the PDP, Honeywell and Cray before they started allowing 8-bit addressable units). I believe, though I'm not sure, that some modern mainframes have variable byte sizes, such as 7, 8, or 9-bit bytes where 9 is the most common, and probably a few embedded architectures as well.

    I work with PCs, but even there it's not wise to assume that a byte is 8 bits or that it will remain that way if it is. The story about Microsoft programmers going through every line of their C compiler's source code because they assumed an int was always going to be 16 bits and the designers had other plans.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 02:59 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  4. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 04:07 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

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