how can I make this program quit after the user writes 0:0:0 in this format

Pages: 12

1 #include <iostream>
2 #include <windows.h>
3 using namespace std;
4
5 int main()
6 {
7 int seconds, hours, minutes, timeinsec;
8 char colon;
9
10 cout << "Enter Time (format 12:59:59)"<< endl;
11 cout << "(0:0:0 to quit):";cin>> hours >> colon>> minutes>> colon >> seconds;
12
13
14 cout << endl;
15
16 timeinsec =(hours * 3600 ) + (minutes * 60) + seconds;
17
18 cout << "seconds = " << timeinsec << endl<<endl;
19
20
21
22
23 string password;
24
25 cout <<"(0:0:0 to quit)" << endl;
26 getline (cin,password,'\n');
27
28 if (password == "0:0:0")
29 {
30 quit(0);
31
32 }
33
34
36
37 return 0;
38 }
39


how can I make this function quit after the user is prompted to writes 0:0:0
please anyone know?

what I mean is I need for it to just close(terminate) automatically after the user wrote 0:0:0

thanks for the help

Last edited on
I want the program to quit after the user writes 0:0:0
what is the correct function? did I do it correct or almost correct?



ummh

1. Get user input Hour,minutes,second only
 
         cin >> hours >> minutes >> seconds;

2. Dont use getline
1
2
	cout <<"(0:0:0 to quit)" << endl;
	cin >> password;

3. im pretty sure you are trying to use exit(0)? instead of quit(0)?
1
2
3
4
5
if (password == "0:0:0")
	{
		cout << "Program has ended" << endl << endl;
		exit(0);
	}


and again you are telling the user that 0:0:0 its to quit at the very beginning and there is no if statement :3

Im no expert but im pretty sure this will work..
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
#include <iostream>
#include <windows.h>
#include <string>

 using namespace std;
 int main()
 {
	int seconds, hours, minutes, timeinsec;
	string password;
	cout << "Enter Time (format 12:59:59)"<< endl;
	cout << "(0:0:0 to quit):";
	cin>> hours >> minutes >> seconds;

	if (password == "0:0:0")
	{
		cout << "Program has ended" << endl << endl;
		exit(0);
	}

	cout << endl;

	timeinsec =(hours * 3600 ) + (minutes * 60) + seconds;

	cout << "seconds = " << timeinsec << endl<<endl;




	

	cout <<"(0:0:0 to quit)" << endl;
	cin >> password;

	if (password == "0:0:0")
	{
		cout << "Program has ended" << endl << endl;
		exit(0);
	}

 

	return 0;
 }


now if you want to make it look more organize and less repetitive use function to check..
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
#include <iostream>
#include <windows.h>


void checkPassword (int, int, int);


 using namespace std;
 int main()
 {
	int seconds, hours, minutes, timeinsec;
	cout << "Enter Time (format 12:59:59(DO NOT INCLUDE COLONS JUST SPACE))"<< endl;
	cout << "(0:0:0 to quit):";
	cin>> hours >> minutes >> seconds;

	checkPassword(hours, minutes, seconds);

	cout << endl;

	timeinsec =(hours * 3600 ) + (minutes * 60) + seconds;

	cout << "seconds = " << timeinsec << endl<<endl;




	

	cout <<"(0:0:0 to quit)" << endl;
	cin >> hours >> minutes >> seconds;

	checkPassword(hours, minutes, seconds);

	return 0;
 }

 void checkPassword (int hour, int minute, int second)
 {
	 
	 if (hour == 0 && minute == 0 && second == 0)
	{
		cout << "Program has ended" << endl << endl;
		exit(0);
	}
 }


you can also take it up a notch and make this repeat (i wont do it) practice :>

none of these worked because first of all it didn't even let me type the password and as soon as I typed the first 0 the program disappeared:(
I want the program to disappear once I type the password 0:0:0
thanks for trying to help thought
dos anyone please know how to actually make this work
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
    int seconds = 0, hours = 0, minutes = 0, timeinsec = 0;
    char colon = ':';
    
    cout << "Enter Time (format 12:59:59) (0:0:0 to quit): ";
    
    while ( cin >> hours >> colon >> minutes >> colon >> seconds )
    {
        timeinsec = ( hours * 3600 ) + ( minutes * 60 ) + seconds;
        
        if(timeinsec == 0)
            break;
        else
            cout << "seconds = " << timeinsec << endl;
    }
    
    cout << "Program ends\n";
    
    return 0;
}
Last edited on
what I'm looking for is for the program to terminate after I write 0:0:0
not for it to display program end
thanks for trying thought but I need it to exit automatically once I type 0:0:0
closed account (48T7M4Gy)
Then delete my line 22.
but it still the program wont exit once I type 0:0:0
I really need help I don't know what else to do to make it do that
What is the current code you're trying to run?
23 string password;
24
25 cout <<"(0:0:0 to quit)" << endl;
26 getline (cin,password,'\n');
27
28 if (password == "0:0:0")
29 {
30 quit(0);
31
32 }
33
34
36
37 return 0;
38 }



I don't know if its correct but the idea is to type 0:0:0 then the program just gos away in other words exit automatically after I type 0:0:0
could you help
Did you try Kemort's code above, without the line 22 printing out the program end message? It seems to do what you're trying to accomplish.
closed account (48T7M4Gy)
If the program I wrote still does not exit then try turning the power off or setting fire to your computer. Sometimes that happens with these complex operating systems and unfortunately that's all you can do.

If that doesn't work send me your computer by courier and I will do it for you. Make sure you send money for the return postage.
I did try with out line 22 but it dosent exit by itself it waits for me to press enter
I cant send my computer need it for school
but what do you mean by set fire to computer
closed account (48T7M4Gy)
Pour petrol on it, strike a match, then kaboom. That should make an effective exit.
Pages: 12