Hi guys im a beginner in c++ and im practicing making a little program in which I input a time in which I want my pc shutdown in a format like hh:mm and the program translates those numbers in to seconds to use the shutdown.exe program.
I have two functions one for hours and another one for minutes they look like this:
1 2 3 4 5 6 7 8 9
int minutes(int x)
{
return x * 60;
}
int hours(int x)
{
return x * 60 * 60;
}
Now I just want to input 1:30
how do i make the computer to know that the 1 will use the hours function and the 30 the minutes function?
I mean how do i make the computer select the left side of the ":" and assign it to my int hh variable and the right side to my int mm variable?
then I can just simply add the values (already converted in seconds) and put that to the shutdown command...
#include <iostream>
int minutes(int x)
{
return x * 60;
}
int hours(int x)
{
return x * 60 * 60;
}
int main()
{
int time;
int hh;
int mm;
std::cout << "please input the time in which you want to shutdown the computer in the format hh:mm";
std::cin >> time;
return 0;
}
PS: of course is not finished i just thought about it!
and yes i know about the downside about using the system(); function -lack of portability, vulnerabilities, etc- is just that im practicing and i decided to do something "easy". :)
cin >> hh // get the first num
cin.ignore(256, ':') // ignore the colon, the 256 could even be a 1, but I put 256 is case the user put a bunch of spaces or something
cin >> mm // get the next number
#include <iostream>
#include <stdio.h>
/* Our functions */
int minutes(int x)
{
return x * 60;
}
int hours(int x)
{
return x * 60 * 60;
}
int main()
{
/* our variables */
int hh;
int mm;
int seconds;
char command [100];
std::cout << "Please input the time in which you want to shutdown the computer in the format hh:mm\n";
std::cin >> hh;
std::cin.ignore(256,':'); /* ignoring the ":" and the spaces */
std::cin >> mm;
seconds = hours(hh) + minutes(mm); /* converting to seconds */
sprintf(command, "shutdown -s -t %i -c \"The computer will shutdown in the specified time\"", seconds);
/* passing our command to a string to be able to include our variables */
system(command); /* passing our comand to the system including the variable input */
return 0;
}