Ok, what I'm trying to do is have the user enter a command at the prompt, and on the right command a function is called.

Here's my code:

#include <iostream>
#include <stdlib.h>

void functionCall()
{
     std::cout << "function successfully called";
     }
     
     
int main()
{
    char userInput [128];
      
    std::cin.getline(userInput, 128);
    
    std::cout << userInput;
    

    if (userInput == "pie")
    {
                  std::cout << "values are equal";
                  functionCall();
                         }
       
    return 0;
}

now, I'll enter "pie" at the prompt, however it just skips over the IF statement and exits. What am I doing wrong?

Recommended Answers

All 2 Replies

Ok, what I'm trying to do is have the user enter a command at the prompt, and on the right command a function is called.

Here's my code:

#include <iostream>
#include <stdlib.h>

void functionCall()
{
     std::cout << "function successfully called";
     }
     
     
int main()
{
    char userInput [128];
      
    std::cin.getline(userInput, 128);
    
    std::cout << userInput;
    

    if (userInput == "pie")
    {
                  std::cout << "values are equal";
                  functionCall();
                         }
       
    return 0;
}

now, I'll enter "pie" at the prompt, however it just skips over the IF statement and exits. What am I doing wrong?

Don't compare a C-String to a word using ==. Use the strcmp function instead:

if (strcmp (userInput, "pie") == 0)

Perfect! Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.