0

So, I have to create a cpp file that uses my script to search for someones name. I'm pretty new to this so I don't know much but this is what I currently have. Also I have to use system(). Here is my code

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main (int argc, char *argv[]) {
    system("./findName.sh" + argv[1] );
    return 0;
}
1
  • You included <string>. Why not create a std::string with the full command you want to pass to std::system? Commented Apr 23, 2020 at 20:24

2 Answers 2

1

The problem is that in C++, string literals (such as "./findName.sh") are not std::string, but pointers. For pointers, addition has another meaning than string concatenation.

You can create a std::string and use it to perform the concatenation:

    std::string scriptName = "./findName.sh";
    std::string command = scriptName + " " + argv[1];

    system(command.c_str());

Don't forget to check the size of argc to make sure there is an argument.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this but it is telling me that the command line arguments are not equal to 1, (I wrote this in the shell script)
@Cholo This should be your main function.
It is in my main function, but it is saying the message, like if it didn't receive my argument
also it tells me "./findName.sh: 1: [: 1: unexpected operator"
0

You can use std::system or boost::process for this purposes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.