shutdown command problem

How correctly put variable in shutdown command?

1
2
3
...
 system("shutdown -s -t //variable//");
...
Can the origin of the variable be a string? Or must it be a numeric value?

If it can be a string, you can do:
1
2
3
4
5
6
7
8
9
#include <string> 
#include <cstdlib>

int main()
{
    std::string variable = "10";
    std::string command = "shutdown -s -t " + variable;
    system(command.c_str()); // c_str() returns a const char* to the string
}


However, if variable needs to be an integer, you have to convert it to a string.

With modern C++11, the answer is easy:
1
2
3
4
5
6
7
8
9
#include <string> 
#include <cstdlib>

int main()
{
    int variable = 10;
    std::string command = "shutdown -s -t " + std::to_string(variable);
    system(command.c_str());
}


pre-C++11:
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
#include <sstream> 
#include <cstdlib>

int main()
{
    std::stringstream ss;
    int variable = 10;
    ss << variable;
    std::string command = "shutdown -s -t " + ss.str();
    system(command.c_str());
}
Last edited on
Variable needs to be integer, I included your second idea but there's still something I messed up.

I'll paste whole code to show you what I'm trying to do (I'm beginner at programing ;) )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <conio.h>

using namespace std;

int hours, minutes, time;
string choice;

int main()
{
    cout << " Hours: ";
    cin >> hours;
    cout <<endl;
    cout << " Minutes: ";
    cin >> minutes;
    cout <<endl;
    cout << " Are you sure? (Y/N): ";
    cin >> choice;

    if(choice == "Y" || "y")
    {
        hours=hours * 3600;
        minutes=minutes * 60;
        time= hours + minutes;
        string command_auto_off = "shutdown -s -t" + to_string(time);
        system(command_auto_off.c_str());
        system("cls");
        cout << " Done!";
    }

    if(choice == "N" || "n")
    {
        exit(0);
    }
    return 0;
}


The problem is with 27th line error: 'to_string' was not declared in this scope


I don't really know how to fix it. Thanks for the reply!
> error: 'to_string' was not declared in this scope

#include <string>
Still the same
std::to_string requires C++11 or later.

This (using a string stream to perform the conversion) will work with legacy C++
and also with broken GNU libraries (on Windows)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

int main()
{
    int hours ;
    std::cout << " Hours: ";
    std::cin >> hours;

    int minutes ;
    std::cout << " Minutes: ";
    std::cin >> minutes;

    char choice ;
    std::cout << " Are you sure? (Y/N): ";
    std::cin >> choice;

    const int secs = hours*3600 + minutes*60 ;

    if( ( choice == 'Y' || choice == 'y' ) && secs >= 0 )
    {
        std::stringstream sstm ;
        sstm << secs ;
        std::string secs_str ;
        sstm >> secs_str ;

        const std::string command_auto_off = "shutdown -s -t " + secs_str ;
        std::system( command_auto_off.c_str() );
        std::cout << " Done!";
    }
}
Everything works, thank you! :)

I'm using Code Blocks, GNU GCC Compiler, that's the problem why previous solution didn't work?
My third option should have worked, but it's pretty much equivalent to JLBorges'.

Regardless, there's some history to this because:
(1) Code Blocks may come with a slightly outdated compiler. I'm not sure if this is still the case, but the default compiler can easily be replaced with your own, up-to-date one -- Code Blocks as an IDE itself is still great.
(2) Outdated versions of the compiler have to set -std=c++11 as an addition command-line argument to g++ to get C++11 to work. There should be a checkbox in Compiler Settings that does this for you.
(3) Outdated versions of MinGW (Which is the GNU GCC Compiler for Windows) have a bug where std::to_string is not recognized. https://stackoverflow.com/questions/12975341/to-string-is-not-a-member-of-std-says-g-mingw

tl;dr I would suggest downloading a more-recent compiler version, and setting the paths in Code Blocks to point to the new compiler's bin folder's .exe's
https://stackoverflow.com/questions/5604183/adding-compiler-to-codeblocks
Last edited on
Topic archived. No new replies allowed.