Question: Write a C++ program that prompts the user to enter a binary number: std::string str_binary; std::cout < < Enter a binary number: ; std::cin >>

Write a C++ program that prompts the user to enter a binary number:

 std::string str_binary; std::cout << "Enter a binary number: "; std::cin >> str_binary; 

The program then prints the value in hexadecimal and decimal format.

If the number is a printable ASCII character, also display the corresponding character.

Repeat the above steps in an endless loop. Exit the program if the user enters "exit" or "quit".

Coding requirements

Store user input in a string variable named str_binary as shown above.

If bad input is detected (the user enters something other than digits 0 or 1), print appropriate error message and go back to the initial prompt.

Use separate functions to

Get user input:

 string get_input( string const& prompt ) { //...  } 

Validate user input. The function returns true if str_binary contains only binary digits, false otherwise.

 bool validate_input( string const& str_binary ) { //...  } 

Convert string input to an int

 int bits_2_int( string const& str_binary ) { //...  } 

Display result in a decimal format

 void display_dec_int( int value ) { //...  } 

Display result in a hexadecimal format

 void display_hex_int( int value ) { //...  } 

Verify whether the int contains the code of a printable character:

 bool is_printable( int ch ) { if ( 0 < ch && ch <= 255 && isprint( ch ) ) { // This is a printable character:  return true; } else { // Not a printable character:  return false; } } 

isprint() is a standard library character classification function. It returns true the character is printable, false otherwise. Notice that we also need to check whether the int is in range between zero and 255, or else isprint() may cause a debugging exception and terminate the program.

Program organization requirements

Your project should have

A separate header file containing declarations of all constant variables and functions (except main()). Note: each header file must have its own unique header guards.

A separate C++ source file that contains the main() function

A separate C++ source file with implementation of the functions (that is, function definitions.)

VERY IMPORTANT: each function must have comments explaining in substantial detail what it does and why. Not following this requirement will result in a deduction of 10 pts per each undocumented function.

Useful Info

Specifying the header guards

The header guards in an arbitrary header file named my_file.h should look like this:

// my_file.h #ifndef MY_FILE_H_INCLUDED_ #define MY_FILE_H_INCLUDED_ // ... the rest of the file goes here ...  #endif MY_FILE_H_INCLUDED_ 

If using copy and paste, don't forget to replace "MY_FILE" with the name of "YOUR_FILE" file!

Accessing characters in a string

When user input is stored in a string named str_binary, you can access individual characters like this:

 // Accessing characters in a string from left to right:  int idx; for ( idx = 0; idx < str_binary.length(); ++idx ) { char one_char = str_binary[ idx ] } 

Likewise, if you need to iterate through characters in the opposite order, right to left,

 // Accessing characters in a string from right to left:  int idx; for ( idx = str_binary.length() - 1; idx >= 0 ; --idx ) { char one_char = str_binary[ idx ] } 

Converting ASCII digit to an integer

To convert character '0' or '1' to the int value 0 or 1, use the following code:

 int one_bit = one_char - '0'; 

Displaying an int in a decimal or hexadecimal format

To print an int in a required format, use one of the following:

 cout << hex << value; // outputs value in hexadecimal format  cout << dec << value; // outputs value in decimal format  

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!