This is a program I made while learning about static type casting. The user must type in a binary byte such as 00011011 and the program will return the ASCII equivalent (unless the ASCII character is undisplayable, such as a NULL character).
What do you think of it and mainly, what improvements do you think I can make on it?
//changed some formatting
#include <iostream>
#include <iomanip>
usingnamespace std;
int main() {
// int bit[8] = { 128, 64, 32, 16, 8, 4, 2, 1 }; //not needed
int bitA = 0;
// int i; //we don't need to define i in this scope
char usr_byte[9];
cout << "Enter byte: ";
cin >> setw(9) >> usr_byte;
//changed the loop a bit, we didn't need usr_byte[8]
//i is now defined only inside the loop
//(removed a few braces and changed a few "" to '')
for( int i = 0; i < 8; i++ )
if( usr_byte[i] == '1' )
bitA += 1 << i; //look up left and right shift operators, i.e << and >>
elseif( usr_byte[i] != '0' ) {
cout << "Invalid didgit at position " << i << '\n';
break;
}
cout << "Byte Numerical Value: " << bitA << '\n';
// char ASC = static_cast<char>(bitA); //you don't need this variable
cout << "ASCII Character Value: " << static_cast<char>(bitA) << '\n'; //just put the cast in here
return 0;
}