I would like to switch between different BYTE Arrays after user input. I need to apply the same processing to different arrays depending on user input, but I would like to avoid repeating the code. One solution could be to create one array ahead of the if
logic and then copys the correct byte pattern into that array. I need the BYTE variable someData in my main function because a followup function needs it for data processing.
Currently I am getting this error : 'someData' was not declared in this scope' This is my code:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
int main()
{
string titel;
std::cout << "CHOOSE PARAM: ";
std::cin >> titel;
if (titel == "PARAM1") {
BYTE someData[17] = { 0x00, 0x87, 0x80, 0x83, 0x10, 0x09, 0x08, 0x00, 0x83, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00 };
}
else if (titel == "PARAM2")
{
BYTE someData[16] = { 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xBE, 0x00, 0x40 };
}
else if (titel == "PARAM3")
{
BYTE someData[8] = { 0x01, 0x01, 0x16, 0xD5, 0x01, 0xD8, 0x00, 0x00 };
}
std::cout << "SomeData: ";
for(int i = 0; i < 23; i++)
{
printf("%02x ", someData[i]);
}
}
I would like to switch between different BYTE Arrays after user input.
As said, the someData
must be declared outside the if
-blocks:
#include <iostream>
#include <string>
#include <cstring>
#include <memory>
#include <Windows.h>
using namespace std;
int main()
{
string titel;
std::cout << "CHOOSE PARAM: ";
std::cin >> titel;
BYTE * someData = 0;
int NsomeData =0 ;
if (titel == "PARAM1") {
BYTE tmp[17] = { 0x00, 0x87, 0x80, 0x83, 0x10, 0x09, 0x08, 0x00, 0x83, 0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00 };
someData = new BYTE[17] ;
memcpy(someData,tmp,sizeof(BYTE[17])) ;
NsomeData = 17 ;
}
else if (titel == "PARAM2")
{
BYTE tmp[16] = { 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xBE, 0x00, 0x40 };
someData = new BYTE[16] ;
memcpy(someData,tmp,sizeof(BYTE[16])) ;
NsomeData = 16 ;
}
else if (titel == "PARAM3")
{
BYTE tmp[8] = { 0x01, 0x01, 0x16, 0xD5, 0x01, 0xD8, 0x00, 0x00 };
someData = new BYTE[8] ;
memcpy(someData,tmp, sizeof(BYTE[8])) ;
NsomeData = 8 ;
}
std::cout << "SomeData: ";
for(int i = 0; i < NsomeData; i++)
printf("%02x ", someData[i]);
delete[] someData ;
}