#include <Windows.h>
#include <iostream>
usingnamespace 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
}
elseif (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
}
elseif (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
}
elseif (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
}
elseif (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
}
}
}
#include <Windows.h>
#include <iostream>
int main()
{
constchar* 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;
elseif (life <= 50)
state = 2;
elseif (life <= 79)
state = 3;
elseif (life <= 99)
state = 4;
elseif (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.
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.