Is it considered bad practice to pass string literals as char*, when an array and not a c-string is expected by the function? From my code:
char ConsoleIO::GetNextChar(string prompt, char* choices, int numChoices) {
// Loop until valid input, or 10 times.
for (int i = 0; i < 10; i++) {
Write(prompt);
char* buffer = new char[200];
cin.getline(buffer, 200);
char choice = tolower(buffer[0]);
if (in(choice, choices, numChoices)) {
return choice;
}
WriteLine("Invalid choice.");
}
// Just return the first choice as the default.
return choices[0];
}
// In main:
char userChoice = ConsoleIO::GetNextChar("Make a selection: ", "234", 3);
It compiles, but is it bad practice?
"234"
is aconst char[]
which cannot be converted to achar *
. You should at least get a warning. \$\endgroup\$