Design and implement a C++ program that creates an 8-byte buffer capable of stor
ID: 3665054 • Letter: D
Question
Design and implement a C++ program that creates an 8-byte buffer capable of storing a 64-bit stream. You program shall allow the user to store and retrieve 8-bit (1 byte) values in each of the 8 location of the buffer. Since users can only enter/retrieve 1-byte values, input shall be restricted to numbers ranging between 0-255.
Also, since the buffer can only support 8 locations, the program shall allow users to enter a location index ranging from 0-7. Any input outside of the ranges specified above shall be deemed invalid, causing the program to display an error message to the user. The program shall execute in a loop, until the user selects an option to exit the program. The main menu of the program shall support the following options:
Display to the console the 64-bit stream
Enter data to the buffer by indicating a position in the buffer and data (1-byte) value
Enter a position to retrieve a data value by entering an index position
Exit program
Explanation / Answer
Program:
// include the required heade4r files
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
// create a structure for the bit stream
struct bit_strm
{
unsigned int bit : 8;
};
struct bit_strm *btstrm;
// main method
void main()
{
// decclare the required variables;
long int indx;
int lp, lk;
unsigned char chr, chd;
// allocate the memory space
btstrm=malloc(sizeof(struct bit_strm)*8*sizeof(char));
// loop code for the allocation, storing and retrieving data
for (indx=0; indx<8; indx++)
{
for(lp=0; lp < sizeof(char)*8; lp++)
{
chd=chr;
chd=chd>>(sizeof(char)*8-lp-1);
chd=chd<<(sizeof(char)*8-1);
lk=chd;
if(lk==0)
{
btstrm[sizeof(char)*8*indx + lp].bit=0;
}else
{
btstrm[sizeof(char)*8*indx + lp].bit=1;
}
}
}
// code to display the data
for (indx=0; indx<8; indx++)
{
for(lp=0; lp < sizeof(char)*8; lp++)
{
cout<<btstrm[sizeof(char)*8*indx + lp].bit;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.