Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Aside from your code not compiling there are a couple of glaring problems. First there is no such thing as <code>void main()</code> in C++. It <strong>must</strong> return an <code>int</code>. The second problem is you are redeclaring <code>buffer</code> as a <code>std::stringstream</code>. The third issue with this is the fact that you are then passing it by value to as the first argument to <code>sprintf</code> which will not work since it's neither a <code>char*</code> or being passed by pointer. The forth problem is you are trying to expand the argument as an integer value (<code>%i</code>) instead of a C style string (<code>%s</code>). You are also have two format specifiers for arguments but only pass one. The result of this is undefined and the second <code>%i</code> can expand to any value.</p> <p>Your code should probably look something like this...</p> <pre><code>int main() { char buffer[200]; char arg1[6]="Hello"; sprintf(buffer, "C:\\system.exe %s", arg1); system(buffer); system("pause"); return 0; } </code></pre> <p>To pass a <code>double</code> as an argument just change the <a href="http://en.cppreference.com/w/cpp/io/c/fprintf" rel="nofollow">print specifier</a> in the format string.</p> <pre><code> double arg1 = 1.0f; sprintf(buffer, "C:\\system.exe %lf", arg1); </code></pre> <p>To pass more than one argument you need to include an additional format specifier and pass the argument to <code>sprintf</code></p> <pre><code> sprintf(buffer, "C:\\system.exe %s %s %i", arg1, arg2string, arg3int); </code></pre> <p>Your second program also has a problem and I don't think it's going to execute the way you expect. As Thomas pointed out in his comment you are concatenating the arguments together and then doing a comparison. If you execute it as <code>system.exe hello there</code> then <code>baci</code> will be <code>hello there</code> which seems incorrect. Since you only want to see if <code>Hello</code> has been sent you should check it in your loop instead of concatenating the string.</p> <pre><code>void main(int argc, char*argv[]) { for(int i = 1; i &lt; argc; i++) if (0 == strcmp(argv[i], "Hello")) cout &lt;&lt; "Francesco, ti mando 4 baci !!!" &lt;&lt; endl; system("pause"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload