Compile troubles c to c++ trans
I am unsure what exactly is wrong with this... It won't compile for me, I translated it from c to C++ (or attempted to)... and yes I'm a beginner. Thanks!
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main( int argc, char * argv[], char* env[]) {
int开发者_如何转开发 i = 0;
cout << "Content-type: text/plain\n\n";
/* print query string only */
cout << "%s\n",getenv("QUERY_STRING");
/* print the while environment */
while (env[i]) cout << "%s\n",env[i++];
return 0;
}
Change these:
cout << "%s\n",getenv("QUERY_STRING");
Into this:
cout << getenv("QUERY_STRING") << endl;
Same goes for the other one too, (EDIT:) like this:
while (env[i]) cout << env[i++] << endl;
If you are using cout
, you should #include <iostream>
. Then it will compile.
This is correct.
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main( int argc, char * argv[], char* env[]) {
int i = 0;
cout << "Content-type: text/plain\n\n";
/* print query string only */
cout << getenv("QUERY_STRING") << endl;
/* print the while environment */
while (env[i]) cout << env[i++] << endl;
return 0;
}
Output with std::cout does not use %s or %d like printf, you have to put the variable or value after <<.
精彩评论