I am trying to make a pointer to a constant character array from a c++ string. In the last four lines, I am adding the three strings to one string. This should be used to create a pointer to a constant array. This pointer should then be returned to be used in another function. When I am debugging step by step, the "cout" at the end of the function shows the correct behaviour. When I am looking at the returned value in the main function, it points to garbage data. What am I doing wrong while returning the pointer?
const char *checkMultiID(void){
string startID = "USB0::0x2A8D::0x0101::";
string usbID = "MY54500604";
string endID = "::0::INSTR";
char answerID;
int correctFunctionInput = 0;
cout << "ID = " << usbID << "? [Y/N]" << endl;
scanf("%c", &answerID);
while(correctFunctionInput == 0){
if ((answerID == 'Y') || (answerID == 'N')){
correctFunctionInput = 1;
}
else{
cout << "Incorrect Input. Please repeat." << endl;
scanf("%c", &answerID);
}
}
if (answerID == 'N'){
cout << "Please Type in the ID like MY..." << endl;
getline (cin, usbID);
}
string fullID = startID + usbID + endID;
const char *idChar = &fullID[0];
cout << idChar << endl;
return idChar;
}