Error when calling strcmp Invalid conversion from 'int' to 'const char *'
I am using strcmp to compare character arrays in C ++, but I am getting the following error for each occurrence of strcmp: error: invalid conversion from 'int' to 'const char *' followed by: error: initialization argument 2 of 'int strcmp (const char *, const char *) '
I have a string, string.h and stdio.h and here is my code, thanks to everyone who answers.
Also, is there a better way to check a buffer other than a bunch of if statements?
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;
}
code>
source to share
Your comparison string is incorrect. They should look like "hist"
, not 'hist'
.
In C ++ 'hist'
is just a character literal (as stated in section 2.14.3 of the C ++ 0x draft standard (n2914)), my emphasis is on the last paragraph:
A character literal is one or more characters enclosed in single quotes, like x, optionally preceding one of the letters u, U, or L, respectively in uy, Uz, or Lx.
A character literal that does not start with u, U, or L is a regular character literal, also called a narrow character literal.
An ordinary character linear character containing one c-char is of type char, the value of which is the numeric c-char encoding value in the execution character set.
An ordinary literal containing more than one c-char is a multichannel literal. A multichannel literal is int with an implementation-defined value.
As for the best way, it depends what you mean better :-)
One possibility is to create a functon table, which is basically an array of structures, each containing a word and a function pointer.
Then you simply extract the word from your string and search that array, calling the function if you find a match. The following C program shows how to use function tables. As for the best solution, I'll leave it to you (this is a moderately advanced method) - you might be better off sticking with what you understand.
#include <stdio.h>
typedef struct { // This type has the word and function pointer
char *word; // to call for that word. Major limitation is
void (*fn)(void); // that all functions must have the same
} tCmd; // signature.
// These are the utility functions and the function table itself.
void hello (void) { printf ("Hi there\n"); }
void goodbye (void) { printf ("Bye for now\n"); }
tCmd cmd[] = {{"hello",&hello},{"goodbye",&goodbye}};
// Demo program, showing how it done.
int main (int argc, char *argv[]) {
int i, j;
// Process each argument.
for (i = 1; i < argc; i++) {
//Check against each word in function table.
for (j = 0; j < sizeof(cmd)/sizeof(*cmd); j++) {
// If found, execute function and break from inner loop.
if (strcmp (argv[i],cmd[j].word) == 0) {
(cmd[j].fn)();
break;
}
}
// Check to make sure we broke out of loop, otherwise not a avlid word.
if (j == sizeof(cmd)/sizeof(*cmd)) {
printf ("Bad word: '%s'\n", argv[i]);
}
}
return 0;
}
When starting with:
pax> ./qq.exe hello goodbye hello hello goodbye hello bork
you get the result:
Hi there
Bye for now
Hi there
Hi there
Bye for now
Hi there
Bad word: 'bork'
source to share