binary input

2 part question: 1)what will the result be if a string(the input)of zeros and ones(01100101)are put through this filter(snippet of code) {i=ch-'0'}in words: i equals ch minus zero. the 1's and 0's are being fetched as characters by getchar. the way I interpret it is: in the ASCII chart char for 0 is 48 decimal an 1 is 49 decimal 2)how do I modify the filter(using ASCII) so that if the input is ab or $# or 01 it only recognizes 01. Im I thinking correctly, or should I be thinking about a print statement which states that the input is no valid. Perhaps I can create a filter which rejects anything from 33 decimal !
to 126 decimal ~ by adding or subtracting 48 decimal 0 or 49 decimal 1 so as to
close in on 0 and 1 from both sides. Your comments would be appreciated.
Why not try it and see ?
> how do I modify the filter(using ASCII) so that if the input is ab or $# or 01 it only recognizes 01.
> Perhaps I can create a filter ...

A filter that would silently discard anything other than '0' and '1' during formatted input would be a custom ctype facet that classifies all other characters as white space.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <locale>
#include <algorithm>

// classify everything other than '0' and '1'as white space
struct only_01 : std::ctype<char>
{

    static const mask* custom_table()
    {
        // start with the table of the classic locale
        static mask custom_tab[table_size] ;
        std::copy( classic_table(), classic_table() + table_size, custom_tab );

        for( auto& m : custom_tab ) m |= space ; // everything is space
        custom_tab[ std::size_t('0') ] &= ~space ; // except '0'
        custom_tab[ std::size_t('1') ] &= ~space ; //    and '1'

        return custom_tab ;
    }

    explicit only_01( std::size_t ref_cnt = 0 )
        : ctype( custom_table(), false /* table was not allocated with new */, ref_cnt ) {}

    struct wrapper
    {
        wrapper( std::istream& stm )
            : stm(stm), old_loc( stm.imbue( std::locale( stm.getloc(), new only_01 ) ) ) {}
        // note: the destructor of the last locale referencing the only_01 facet will delete
        // the facet (ie. the facet will be deleted when it is no longer in use by any locale)

        ~wrapper() { stm.imbue(old_loc) ; }

        // the wrapper is non-copyable, non-moveable, non-assignable
        wrapper( const wrapper& ) = delete ;
        wrapper( wrapper&& ) = delete ;

        std::istream& stm ;
        const std::locale old_loc ;
    };
};

int main()
{
    {
        // std::cin treats everything accept '0' and '1' as white space within this block
        only_01::wrapper wrap(std::cin) ;

        // std::cin >> ch ; would ignore all characters except '0' and '1'
        for( int i = 0 ; i < 5 ; ++i ) { char ch ; std::cin >> ch ; std::cout << ch ; }
    }

    // default input behaviour of std::cin is now restored
    std::cout << "\n---------------------------\n" ;
    for( int i = 0 ; i < 5 ; ++i ) { char ch ; std::cin >> ch ; std::cout << ch ; }
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/bfee11455ee249a4
Topic archived. No new replies allowed.