Converting binary string to ascii

So my project is to convert binary string into ascii or convert ascii into binary string. However, when the program is checking to see if the input binary string is valid or not, it either rejects good or bad inputs when I use && or accepts bad input when I use ||


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void check_bin_number(std::string line)
{
    bool check = false;
    
    while (check != true)
    {
        for (int i = 0; i < line.length(); i++)
        {
            if (line.length() % 8 == 0 && (line.at(i) == '0' || line.at(i) == '1')) //Here is the problem. 
            {
                bin_to_ascii(line);
                check = true;
                break;
            }
            else
            {
                std::cout << "ERROR." << std::endl;
                get_user_input();
            }
        }
    }
}


output when used "||"

Do you want to encrypt or decrypt a message? decrypt
Enter string: 12345678
Your input in ASCII is: 
Your input in ASCII is: 


output when used "&&"

Do you want to encrypt or decrypt a message? decrypt
Enter string: 12345678
ERROR.
Enter string: 09876543
ERROR.
Enter string: 011010100110010101101110
ERROR.
Enter string:
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool valid_bin_number( std::string line )
{
    if( line.length() % 8 != 0 ) return false ;

    // http://www.stroustrup.com/C++11FAQ.html#for
    for( char c : line ) if( c != '0' && c != '1' ) return false ;

    return true ;
}

std::string get_bin_number()
{
    std::string str ;
    std::cout << "enter binary string (number of bits must be a multiple of 8): " ;
    std::cin >> str ;
    if( valid_bin_number(str) ) return str ;

    std::cout << "error in input. try again\n" ;
    return get_bin_number() ;
}
Registered users can post here. Sign in or register to post.