Skiping steps in while loop

Hello. So I was messing around trying to make a shutdown timer for my computer. I want to have a way to stop it if I need, and the method I use doesn't really do that.I want the while loop I use to keep running and stop when I enter a certain text. I would appreciate any help :D .
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 <stdlib.h>

using namespace std;

int main()
{
int timer, hours, minutes, seconds;
string stop;

system("echo off");
system("color 0F");
system("CLS");

label:
cout<<" Automatic shutdown in : (h/m/s) "<<'\n';
cin>>hours;
cin>>minutes;
cin>>seconds;
cout<<" Computer will shutdown in "<<hours<<" hour(s), "<<minutes<<" minute(s) and "<<seconds<<" second(s)."<<'\n';
timer=seconds+minutes*60+hours*3600;

while (timer) // i want this while to keep running, not waiting for me to type something and then timer--
    {
     cin>>stop;
     if ((stop=="stop")||(stop=="STOP")) break;
     else timer--;
    }

string s1, s2; // didn't know how to use goto along with the system("pause>nul") command so I kinda improvised
if (timer==0) system("shutdown /s /f /t 1");
else cout<<" Press any key to return to Shutdown timer input "<<'\n';
cin>>s1;
if (s1!=s2) goto label;

return 0;
}
Great question. This option is system specific generally. I am assuming windows based on your code. MS has an example on sending break commands and such here.
https://msdn.microsoft.com/en-us/library/ms685049%28VS.85%29.aspx
Example with standard C++
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <atomic>
#include <condition_variable>
#include <iostream>
#include <chrono>
#include <thread>

void shutdown()
{
	system("start shutdown /s /f /t 0");
}

std::mutex mut;
std::atomic_flag do_shutdown = ATOMIC_FLAG_INIT;

std::condition_variable wake_up;

struct delayed_shutdown
{
	template <typename TIME_POINT>
	void operator()(TIME_POINT s_time)
	{
		std::unique_lock<std::mutex> lock(mut);
		if(!wake_up.wait_until(lock, s_time, []{ return !do_shutdown.test_and_set(); })) {
			shutdown();
		}
	}
};


int main()
{
	using wall_clock_t = std::chrono::system_clock;

	int h, m, s;
	char skip;
	std::cout << "Automatic shutdown in : (h/m/s)\n";
	std::string input;
	std::cin >> h >> skip >> m >> skip >> s;
	wall_clock_t::time_point when = wall_clock_t::now() +
	                                std::chrono::hours(h) +
	                                std::chrono::minutes(m) +
	                                std::chrono::seconds(s);
	do_shutdown.test_and_set();
	std::thread stop(delayed_shutdown(), when);

	std::cout << " Computer will shutdown in " << h <<" hour(s), " << m <<
	             " minute(s) and " << s << " second(s)." << '\n';
	std::cout << "Press enter to stop shutdown timer";
	std::cin.ignore(128, '\n');
	std::cin.get();
	
	do_shutdown.clear();
	wake_up.notify_one();
	stop.join();
}
Thank you guys for the help, I really appreciate it. I don't understand more than 15% of MiiNiPaa's code :s. The thing is I'm new to this, our class only learn how to use cin, cout, a little bit of <fstream> library, <cmath> library, for loop, while loop and that's kinda it. I am a bit ahead because i like c++ but I still don't know what I'm doing :d.
Last edited on
Topic archived. No new replies allowed.