selective input?

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...

Or at least thats the idea.

thanks for the help!

this is the complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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". :)
Last edited on
1
2
3
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 
Last edited on
I didnt know that if you use the cin.ignore you could assign the next value after the ignored sign to another variable! it works perfectly. :)

thanks for your help!

this is the finished 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
#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;
}
Last edited on
Topic archived. No new replies allowed.