My program is in an infinite loop, when It hits the "Bye" part in the code it goes right back up to the top. Help with this. It's a pretty simple program so I can't see what I did wrong.

#include <iostream>
#include <windows.h>

using namespace std;

int main () {
    cout<<"Shutdown sequences initiated! \n";
    cout<<"3";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<"2";
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<"1";
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<".";
    Sleep(250);
    cout<<"Bye!";
    system("shutdown -s");
    
    

return 0;
}

1. For C++, use endl manipulator instead of \n for newlines.
2. The shutdown command has to run as root.
3. There is no loop here, so it should not start over, unless you named your program "shutdown"...

1. For C++, use endl manipulator instead of \n for newlines.
2. The shutdown command has to run as root.
3. There is no loop here, so it should not start over, unless you named your program "shutdown"...

What do you mean by as a root?
I had the same line of code being the system shutdown part and it worked before, can you give an example?


Nevermind got it working. Just had to change the name... Lol.

If you run this code on a server, you must be a root - administrator - to do the command. What purpose are you trying to do here? A plank???

This is kind of an abstract idea but what if your problem is that your trying to get the program to return a value after you have sent the shutdown command. And when searching on google myself trying to shut the computer off from c++ it seems like a difficult thing to do. (Man it was really this simple?) Sorry it's probably not the right answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.