gpt4 book ai didi

c++ - system() 不在 for 循环 C++ 中工作

转载 作者:行者123 更新时间:2023-11-28 01:32:57 28 4
gpt4 key购买 nike

我无法让 system() 在 c++ 的 for 循环中工作。这是我的代码的一个示例:

string modules[13] = {"requests", "pygame", "pronouncing",
"nltk", "pip", "webbrowser",
"uszipcode", "geopy", "Nominatim",
"bs4", "autocorrect", "spell",
"speechrecognition"};
int main() {
for (int i = 0; i < 4; i++)
system("sudo pip3 install " << modules[i]) // This is what raised the error.
}

我收到这个错误:

Documents/JoeInstaller.cpp:109:9: error: no matching function for call to 'system'
system("sudo pip3 install " << modules[i])
^~~~~~

最佳答案

#include <string>
#include <cstdlib>

std::string modules[13] = {"requests", "pygame", "pronouncing",
"nltk", "pip", "webbrowser",
"uszipcode", "geopy", "Nominatim",
"bs4", "autocorrect", "spell",
"speechrecognition"};

int main() {
std::string sudo("pip3 install ");
for (int i = 0; i < 13; i++)
std::system( (sudo + modules[i]).c_str() );
}

这是一种实现方式,System只有一个参数。

还要避免为每个操作要求密码,请使用 sudo 运行 C++ 程序。

关于c++ - system() 不在 for 循环 C++ 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50779606/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com