user3955666
user3955666

Reputation:

Open Application In C

Is there a way I can open an application from a simple command in C?

This is my current code:

int main()
{
    char input;

    printf("Hello! How may I help you? \n");
    scanf("%s", input);

    if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome"){
        printf("Sure! Opening Google Chrome...");
        system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
        printf("Done!");
    }

    return 0;
}

But all it does is tell me that my program has stopped working and quits...

Upvotes: 2

Views: 7887

Answers (4)

ameyCU
ameyCU

Reputation: 16607

Reason your program crash is this -

char input;              // char variable can hold single character
printf("Hello! How may I help you? \n");
scanf("%s", input);             // %s expects char * you pass a char 

Declare input as an array -

char input[100];
...
fgets(input,100,stdin);    // don't use scanf as before suggested  
char *position;
if ((position = strchr(input, '\n')) != NULL)     
  *position= '\0';                             // to remove trailing '\n' 

And this - system("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"); should be written as -

system("Start C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");

This -

if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google 

is not going to work . Use strcmp to compare strings -

if (strcmp(input,"Open Google Chrome") == 0)    // similarly compare other strings.

Upvotes: 8

Coffee'd Up Hacker
Coffee'd Up Hacker

Reputation: 1446

There are a few things wrong with your code, others have pointed them out already so I won't. I, however, want to post how I would go about doing what you want.

Here it is...

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int
main (void)
{
    char input[100];

    char *chrome_answers[] = {
        "Open Google Chrome",
        "open google chrome",
        "open chrome",
        "run google chrome",
        "run chrome",
        "Run Google Chrome",
        "Run Chrome",
        "Open Chrome"
    };

    int chrome_length = sizeof (chrome_answers) / sizeof (*chrome_answers);
    int i;

    printf ("Hello! How may I help you? \n");
    scanf ("%99[^\n]s", input);

    for (i = 0; i < chrome_length; i++) {
        if (strcmp (input, chrome_answers[i]) == 0) {
            printf ("Sure! Opening Google Chrome... ");
            fflush (stdout);
            system ("Start C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
            printf ("Done!\n");

            break;
        }
    }

    return 0;
}

The main thing is I would use an array of possible valid "answers", instead of a long "if statement" like you have in your code, which just gets even longer when you use strcmp as needed.

Upvotes: 0

Spikatrix
Spikatrix

Reputation: 20244

Your code has several problems:

  1. You have char input; which can store a single char. But you want to store a string. So, change that into an array of some size:

    char input[64];
    
  2. You have scanf("%s", input);. %s stops on encountering a whitespace character. In order to read spaces upto a newline character('\n'), use the %[ format specifier:

    scanf("%63[^\n]%*c", input); /* %*c is not required; It discards the \n character */
                                 /* %[^\n] scans everything until a '\n' */
                                 /* The 63 prevents Buffer overflows */
                                 /* It is better to check scanf's return value to see if it was successful */
    
  3. You have if(input == "Open Google Chrome" || "open google chrome" || "open chrome" || "run google chrome" || "run chrome" || "Run Google Chrome" || "Run Chrome" || "Open Chrome"){. This doesn't do what you expect. It compares whether the address of the first element of input has the same address of that of the string literals. Also, chaining OR operators like that doesn't do what you expect.

    You'll need to include string.h and use strcmp:

    if(strcmp(input, "Open Google Chrome") == 0 || strcmp(input, "open google chrome") == 0 || strcmp(input, "open chrome") == 0 || strcmp(input, "run google chrome") == 0 || strcmp(input, "run chrome") == 0 || strcmp(input, "Run Google Chrome") == 0 || strcmp(input, "Run Chrome") == 0 || strcmp(input, "Open Chrome") == 0){
    
  4. Also, as other people have pointed out, you need to escape the \ character in the system function by using \\ instead of \. You might need to enclose the whole thing in double quotes("...") too.

Upvotes: 8

king
king

Reputation: 507

You can't do it like that. A char is supposed to contain only one character like 's' or 't' or '5'. "open google chrome" is a chain of characters. If you want to compar the string input with "open google chrome" for example you need to create an array like that: char[10] input; for example, it will create a array that can contain 10 characters. Then to compar each character you need to do something like: char *str = "open google chrome" and then compar each charater by character: if (input[0] == str[0])

But you already have some function in the library that does that for you (strcmp I think). And you can't do: if (intput[0] == 'r' || 't' || 'v') you need to do: if (intput[0] == 'r' || input[0] == 't' || input[0] == 'v')

This is a global idea maybe there are some mistakes, it's been a while I didn't touch C lol.

Hope it helps.

Upvotes: 0

Related Questions