tags:

views:

29

answers:

3
#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.

A: 

You have to escape your quotation marks and format the string. Another way of doing this would be:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    short int enterVal;
    char command[128];
    cout << "enter a number to say: " << endl;
    cin >> enterVal;
    snprintf((char *)&command, 128, "say \"%d\"", enterVal);
    system(command);
    return 0;
}

You should also be aware that you should programatically avoid using system() calls as this makes your program vulnerable to security flaws.

If you're just messing around and don't mind then continue by all means ;)

coffeebean
Yeah, this program is just for fun. :)
Alex
+3  A: 

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());
}
avakar
Thanks, works great!
Alex
A: 

You could use something like this:

#include <iostream>
#include <sstream>
using namespace std;

int main() {

    short int enterVal;
    cout << "enter a number to say: " << endl;
    cin >> enterVal;
    ostringstream buff;
    buff << "say " << enterVal;
    system(buff.str().c_str());

    return 0;
}
Jerry Coffin