Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 424881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:19:26+00:00 2026-05-12T19:19:26+00:00

I’m using strcmp to compare character arrays in c++, but I get the following

  • 0

I’m using strcmp to compare character arrays in c++, but I get the following error for every occurrence of strcmp: error: invalid conversion from ‘int’ to ‘const char*’ followed by: error: initializing argument 2 of ‘int strcmp(const char*, const char*)’

I’ve include string, string.h, and stdio.h and here is my code, thanks to all who reply.

Also, is there a better way to check the 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;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:19:27+00:00Added an answer on May 12, 2026 at 7:19 pm

    Your comparison string are incorrect. They should be of the form "hist", not 'hist'.

    In C++, 'hist' is simply a character literal (as stated in section 2.14.3 of the C++0x draft (n2914) standard), my emphasis on the last paragraph:

    A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by one of the letters u, U, or L, as in u’y’, U’z’, or L’x’, respectively.

    A character literal that does not begin with u, U, or L is an ordinary character literal, also referred to as a narrow-character literal.

    An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

    An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.

    As to there being a better way, it depends on what you mean by better 🙂

    One possibility is to set up a functon table which is basically an array of structs, each containing a word and a function pointer.

    Then you simply extract the word from your string and do a lookup in that array, calling the function if you find a match. The following C program shows how to use function tables. As to whether that’s a better solution, I’ll leave it up to you (it’s a moderately advanced technique) – you may 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's 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 run with:

    pax> ./qq.exe hello goodbye hello hello goodbye hello bork
    

    you get the output:

    Hi there
    Bye for now
    Hi there
    Hi there
    Bye for now
    Hi there
    Bad word: 'bork'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.