Could you guys please help me with this C++ problem. All 6 cases should pass. Th
ID: 3844450 • Letter: C
Question
Could you guys please help me with this C++ problem. All 6 cases should pass. Thanks
You program shall allow the user to store and retrieve 8-bit values in each of the 8 location of the buffer. Since users can only enter and 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 Menu 1) View Buffer Content 2) Enter Data (0-255) 3) Retrieve Data 4) Exit program Enter option: Your code should create an alias named buffer for the type unsigned long long The buffer will be of type buffer (i.e., an alias)Explanation / Answer
Hi,
Please find below the C++ code for this requirement-
C++ code-
#include <iostream>
#include <string>
#include <sstream>
#include <climits>
#include <bitset>
using namespace std;
const size_t NUM_BITS(64);
const size_t NUM_BYTES = NUM_BITS / CHAR_BIT;
const char comma(',');
void getInt(const string&, int *a, int aMin, int aMax, char delim = comma,
int *b = NULL, int bMin = 0, int bMax = 0);
bool isInRange(int, int, int);
const string menu(" 1. _View_Buffer_Content 2. _Enter_Data_(0-255) 3. _Retrieve_Data 4, _Exit_Program > ");
const int menuMin(1), menuMax(4);
const int display(1), setByte(2), getByte(3), quit(menuMax);
int main(int argc, char *argv[]) {
string pSet, pGet;
stringstream ss;
unsigned char bitStream[NUM_BYTES] = { 0 };
int menuSel, byte, value;
ss << "Set: enter byte [0 .. " << NUM_BYTES-1 << "], value [0 .. " << UCHAR_MAX << "]: ";
pSet = ss.str();
ss.clear(); ss.str("");
ss << "Get: enter byte [0 .. " << NUM_BYTES-1 << "]: ";
pGet = ss.str();
do {
getInt(menu, &menuSel, menuMin, menuMax);
if (menuSel != quit) {
switch (menuSel) {
case display:
for (size_t i = 0; i < NUM_BYTES; cout << bitset<CHAR_BIT>(bitStream[i++]));
cout << endl;
break;
case setByte:
getInt(pSet, &byte, 0, NUM_BYTES-1, comma, &value, 0, UCHAR_MAX);
bitStream[byte] = value;
break;
case getByte:
getInt(pGet, &byte, 0, NUM_BYTES-1);
cout << bitset<CHAR_BIT>(bitStream[byte]) << endl;
break;
}
}
} while (menuSel != quit);
return 0;
}
void getInt(const string &s, int *a, int aMin, int aMax, char delim, int *b, int bMin, int bMax) {
string in;
stringstream ss;
char c;
if (a != NULL) {
if (b != NULL) {
do {
cout << s;
getline(cin,in);
ss.clear(); ss.str(in);
} while (!(ss >> *a >> c >> *b) || (c != delim) || !isInRange(*a, aMin, aMax) || !isInRange(*b, bMin, bMax) || ss.good());
} else {
do {
cout << s;
getline(cin,in);
ss.clear(); ss.str(in);
} while (!(ss >> *a) || !isInRange(*a, aMin, aMax) || ss.good());
}
}
}
bool isInRange(int a, int x, int y) {
return (a >= x) && (a <= y);
}
Thanks and Regards,
Vinay Singh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.