下面是我的代码(C++)以下...我很困惑如何将我在getusername
中检索到的用户名返回到主函数。有没有人有没有提示或建议?如何将字符串返回到主int函数?
#include <iostream>
//For the strings obviously
#include <string>
using namespace std;
string getusername();
string user;
string getusername() {
user = system("echo %username% > NUL");
return user;
}
int main() {
string getname;
getname = getusername();
cout << "Hello: "<< getname << endl;
}
我正在使用Visual Studio,它没有返回任何错误...输出永远不会返回用户名,尽管...它只是说'你好:'并且省去了用户名。 – NerdOfCode
您应该阅读关于['std :: system()'](http://en.cppreference.com/w/cpp/utility/program/system)函数实际上的功能 – user0042
您是否想要获取环境变量'%username%'?检查['std :: getenv()'](http://en.cppreference。com/w/cpp/utility/program/getenv)。 – user0042