I am attempting to write a program that interfaces with Command prompt (simple, bring up a tasklist, user enters the program they want to get rid of, and then the computer does the rest), I already have been able to interface and bring up a tasklist, the code compiles with zero errors or warnings, however, when I enter the debug prompt, and put in the program I want to get rid of, command prompt, not the compiler comes up with the error "ERROR: Invalid syntax. Value expected for '/im'. Type "TASKKILL /?" for usage." I have already done several manual taskkills in regular command prompt, all of which with the same syntax.

tl;dr: Strcat doesn't put the second string where command prompt likes it

Here's the code, just for all of you to look it over:

#include <iostream>
#include <windows.h> //Includes command prompt
#include <string.h>
#include <string>


using namespace std;

int main()
{
    char str [600];
    char task1[20] = "tasklist";
    char task2[20] = "color F9";
    char task3[30];
    int change1;
    system (task2); // Changes the color by entering color F9 into CMD
    cout << "Welcome to the CMD interface with C++ test!\n";
    cout << "To begin testing, press one and then press ENTER.\n";
    cout << "To change the color of this window, press two and then press ENTER.\n";
    cout << "For help, press three and then press ENTER.\n";
    cin >> ( change1 );
    switch(change1) {
        case 1: // begins the task killer
            system(task1); // Enters Tasklist into CMD
            cout << " " << endl;// gets an extra line of text between tasklist and the next line
            cout << "This is a list of all of the tasks currently running by your computer.\n";
            cout << "To stop running a program, please insert the FULL NAME (or the number next to\n";
            cout << "the program) and then press ENTER\n";
            cout << "WARNING: when the program is terminated, it WILL NOT save any of your data,\n";
            cout << "please make sure that all of your data is saved before continuing!\n";
            cin.getline (task3, 30);
            cin.get ();
            strcpy(str, "taskkill /f /im");
            strcat(str, task3);
            cout << "If you are sure you want to exit out of program";
            cout << task3;
            cout << ".\n Then please press Enter";
            system (str);
            cout << "\nPress ENTER to continue.";
            cin.get ();
        //case 2:
        //I have yet to program this part onwards!
        //default:
        cout << "\nIncorrect entry, please try again.";
    }
    return 1;
}

After briefly looking at your code.. maybe all it needs is just a space in between commands:

//this
strcpy(str, "taskkill /f /im");
//might work if it was like this:
strcpy(str, " taskkill /f /im");

After briefly looking at your code.. maybe all it needs is just a space in between commands:

//this
strcpy(str, "taskkill /f /im");
//might work if it was like this:
strcpy(str, " taskkill /f /im");

I just tried this, unfortunately, it still didn't work, I added a line to the code to make it output the string containing the program name (the string is named task3), and it failed to print anything, so my belief is that I did something incorrect in this area. (lines 31 and 32)

cin.getline (task3, 30);
cin.get ();

That would explain why I needed to have a wait ( cin.get () ) so that it didn't go ahead to strcpy, and the user had time to insert the program

this might work..

strcpy(str, "taskkill /f /im "); //adding a space character after an options..     
strcat(str, task3);

this might work..

strcpy(str, "taskkill /f /im "); //adding a space character after an options..     
strcat(str, task3);

The syntax of the taskkill line has been changed to your value, however, the problem still remains, there is obviously a problem in the cin.getline function, for, as I have previously stated, It does not output when I do a simple function such as

cin.getline (task3, 30); //get value of task3
cout << task3; //output value of task3

The code fails to re-print task3. The only problem I can think of is an incorrect usage of cin.getline ().
Other than adding the "checker" for the string and a space after the taskkill line, I have changed nothing else with the original code.

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.