C++;在变量给定的指定时间范围内关闭电脑 我尝试使用不同代码类型的代码变体在一个变量告诉C++时关闭我的计算机。它不起作用 system("shutdown -s -t %Charseconds"); system("shutdown -s -t %Intseconds"); system("shutdown -s -t %Stringseconds"); system("shutdown -t %Charseconds"); system("shutdown -t %Intseconds"); system("shutdown -t %Stringseconds");

C++;在变量给定的指定时间范围内关闭电脑 我尝试使用不同代码类型的代码变体在一个变量告诉C++时关闭我的计算机。它不起作用 system("shutdown -s -t %Charseconds"); system("shutdown -s -t %Intseconds"); system("shutdown -s -t %Stringseconds"); system("shutdown -t %Charseconds"); system("shutdown -t %Intseconds"); system("shutdown -t %Stringseconds");,c++,windows,operating-system,system,C++,Windows,Operating System,System,最小可重复性: TurnOff(int seconds) { seconds = 10; string str = to_string(seconds); system("shutdown - s - t " + str); } 错误:从“std::basic_string”到“const char*”没有合适的转换函数 system()不知道如何格式化这样的参数。它接受一个C样式的char*字符串,并将其原样传递给cmd.exe(作为/C开关的参

最小可重复性:

TurnOff(int seconds) 
{
    seconds = 10;
    string str = to_string(seconds);
    system("shutdown - s - t " + str);
}
错误:从“std::basic_string”到“const char*”没有合适的转换函数

system()
不知道如何格式化这样的参数。它接受一个C样式的
char*
字符串,并将其原样传递给
cmd.exe
(作为
/C
开关的参数)。因此,在将字符串传递给
system()
之前,您有责任使用变量输入格式化字符串

您可以使用
c_str()
方法将
std::string
传递给
system()
,例如:

void TurnOff(int seconds)
{
    std::string str = "shutdown -s -t " + std::to_string(seconds);
    system(str.c_str());
}

如所说,考虑使用调用来关闭,而不是使用<代码>系统()/代码> .< /p> < p>“代码>关机-s+t“+STR >是<代码> STD::String 。<代码>系统< /代码>期望一个常数C字符串(<代码> const char */COD>)。您必须转换它,例如:

system(("shutdown - s - t " + str).c_str());

@PageUp表示您没有正确地使用它,但您没有提供一个确切的演示,说明什么不适合您。@PageUp所有人都可以根据您所展示的内容来回答问题。如果您所展示的内容明显错误,您将得到明显的答案或一次钓鱼探险,以找出您真正的错误。@PageUp您需要创建一个页面并将其发布到中你的问题提交了edit@PageUp我已经在我的答案示例中介绍了该错误的解决方案。您需要使用
std::string::c_str()
。我已更新了我的答案,以便在您的编辑上下文中显示这一点