Search code examples
c++macosbashsystem

Getting input in system() function (Mac)


#include <iostream>
using namespace std;

int main() {

    short int enterVal;
    cout << "enter a number to say: " << endl;
    cin >> enterVal;
    system("say "%d"") << enterVal;

    return 0;
}

Is what I am currently trying. I want the user to enter a number and the system() function says it basically. The code above has an error which says " 'd' was not declared in this scope ". Thanks in advance.


Solution

  • You must format the string manually.

    #include <iostream>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        short int enterVal;
        cin >> enterVal;
    
        stringstream ss;
        ss << "say \"" << enterval << "\"";
        system(ss.str().c_str());
    }