我正在使用 strcmp 来比较 C++ 中的字符数组,但每次出现 strcmp 时都会出现以下错误:错误:从 'int' 到 'const char*' 的无效转换,后跟:错误:初始化参数 2 of 'int strcmp (常量字符*,常量字符*)'
我已经包含了 string、string.h 和 stdio.h,这是我的代码,感谢所有回复的人。
此外,除了一堆 if 语句之外,还有更好的方法来检查缓冲区吗?
int main(int argc, char* argv[])
{
unsigned count = 0;
bool terminate = false;
char buffer[128];
do {
// Print prompt and get input
count++;
print_prompt(count);
cin.getline(buffer, 128);
// check if input was greater than 128, then check for built-in commands
// and finally execute command
if (cin.fail()) {
cerr << "Error: Commands must be no more than 128 characters!" << endl;
}
else if ( strcmp(buffer, 'hist') == 0 ) {
print_Hist();
}
else if ( strcmp(buffer, 'curPid') == 0 ) {
// get curPid
}
else if ( strncmp(buffer, 'cd ', 3) == 0 ) {
// change directory
}
else if ( strcmp(buffer, 'quit') == 0 ) {
terminate = true;
}
else {
//run external command
}
} while(!terminate);
return 0;
}