battery

Finally finished my code that reads the percentage of a computer battery and tells you the status and the percentage every 10000 seconds

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
#include <Windows.h>
#include <iostream>

using namespace std;

int main()
{
	while (true)
	{
		SYSTEM_POWER_STATUS status;
		GetSystemPowerStatus(&status);

		int life = status.BatteryLifePercent;

		cout << "Battery status is:";

		if (life <= 20) //if % is less than or equal to 20
		{
			cout << " low, your should consider charging your battery" << endl << "your batter percentage is " << life << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 1000 seconds
		}
		else if (life >= 21 && life <= 50) //if % is greater than or equal to 50 and less than or equal to 79
		{
			cout << " ok, should still consider charging your battery" << endl << "your batter percentage is " << life << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 1000 seconds
		}
		else if (life >= 51 && life <= 79) //if % is greater than or equal to 50 and less than or equal to 79
		{
			cout << " average, should still consider charging your battery, but not needed" << endl << "your batter percentage is " << life << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 1000 seconds
		}
		else if (life >= 80 && life <= 99) //if % is greater than or equal to 80 and less than or equal to 99
		{
			cout << " good, shouldnt need to charge your battery" << endl << "your batter percentage is " << life << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 1000 seconds
		}
		else if (life == 100) //if % is egual to 100
		{
			cout << " great, battery is full" << endl << "your batter percentage is " << life << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 10000 seconds
		}
		else//if no reading
		{
			cout << " not working, or no battery" << endl;
			Sleep(10000);//wait 10000 seconds
			continue;//restart program after 10000 seconds
		}
	}
}
can i have unix version for the same program...?
Slightly modified your code:
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
#include <Windows.h>
#include <iostream>


int main()
{
    const char* status_string[] =
        {
         "not working, or no battery",
         "low, your should consider charging your battery",
         "ok, should still consider charging your battery",
         "average, should still consider charging your battery, but not needed",
         "good, shouldnt need to charge your battery",
         "great, battery is full",
        };
    SYSTEM_POWER_STATUS status;
    while (true) {
        GetSystemPowerStatus(&status);
        int life = status.BatteryLifePercent;
        int state = 0;
        /**/ if (life <= 20)
            state = 1;
        else if (life <= 50)
            state = 2;
        else if (life <= 79)
            state = 3;
        else if (life <= 99)
            state = 4;
        else if (life == 100)
            state = 5;

        std::cout << "Battery status is: " << status_string[state] << std::endl;
        if (state != 0)
            std::cout << "Your battery percentage is " << life << std::endl;
        Sleep(10000);
    }
}
You can also look into BatteryFlag variable to see if your device is currently charging and if there a battery present and so on.
Last edited on
Ya I probably will add the status of charging or not I didn't use cases because I haven't used them yet
Just get visual studios for UNIX I'm not a UNIX user so I may be wrong






http://msdn.microsoft.com/en-us/library/xw1ew2f8.aspx
Just get visual studios for UNIX

Sorry. Microsoft does not support Visual Studio on Unix. The page you linked is meant to help Unix developers get up to speed using Visual Studio on Windows.
Ok I'll look into making a UNIX version
Topic archived. No new replies allowed.