My time converter - trying to handle uncorrect input!

Well I made a simple time converter, anyways I am trying to get it to where, lets say the user types in D but the options are only 1-10 I want it to not close and say error. It works if I type in a number not between 1-10 BUT it won't work for anything else :| Also how can I get the application to exit in 5 seconds on it's own? I tried
exit(5000); but that didn't work :| Should I try
1
2
sleep(5000);
exit(1);

?

Here is my source:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// TimeConverter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int main()
{
	system("COLOR 02");
	system("TITLE Time Converter by CometJack");

	char chrRep, chrTest;
	short lngVal, lngOpt;

	system("CLS");
	std::cin.clear();
	std::cout << "Welcome to the Time Converter, what would you like to convert?" << std::endl;
	std::cout << "1) Minutes -> Hours" << std::endl << "2) Seconds -> Hours" << std::endl << "3) Milliseconds -> Hours" <<
		std::endl << "4) Seconds -> Minutes" << std::endl << "5) Milliseconds -> Minutes" << std::endl << "6) Months -> Years" <<
		std::endl << "7) Weeks -> Years" << std::endl << "8) Days -> Years" << std::endl << "9) Minutes -> Years" <<
		std::endl << "10) Hours -> Years" << std::endl;
	std::cin >> lngOpt;

	switch (lngOpt)
	{
	case 1:
		system("CLS");
		std::cout << "How many hours?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (60*lngVal) << " minutes in " << lngVal << " hours." << std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (60) << " minutes in 1 hour." << std::endl;
		}
		else
		{
			std::cout << "There are 0 minutes in 0 hours." << std::endl;
		}
		break;
	case 2:
		system("CLS");
		std::cout << "How many hours?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << ((60*60)*lngVal) << " seconds in " << lngVal << " hours." 
				<< std::endl;
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (60*60) << " seconds in 1 hour." << std::endl;
		}
		else
		{
			std::cout << "There are 0 seconds in 0 hours." << std::endl;
		}
		break;
	case 3:
		system("CLS");
		std::cout << "How many hours?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (((1000*60)*60)*lngVal) << " milliseconds in " << lngVal << " hours." 
				<< std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << ((1000*60)*60) << " milliseconds in 1 hour." << std::endl;
		}
		else
		{
			std::cout << "There are 0 milliseconds in 0 hours." << std::endl;
		}
		break;
	case 4:
		system("CLS");
		std::cout << "How many minutes?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (60*lngVal) << " seconds in " << lngVal << " hours." << std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (60) << " seconds in 1 hour." << std::endl;
		}
		else
		{
			std::cout << "There are 0 milliseconds in 0 hours." << std::endl;
		}
		break;
	case 5:
		system("CLS");
		std::cout << "How many minutes?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (1000*lngVal) << " milliseconds in " << lngVal << " minutes." 
				<< std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (1000) << " milliseconds in 1 minute." << std::endl;
		}
		else
		{
			std::cout << "There are 0 milliseconds in 0 minutes." << std::endl;
		}
		break;
	case 6:
		system("CLS");
		std::cout << "How many years?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (12*lngVal) << " months in " << lngVal << " years." << std::endl;
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (12) << " months in 1 year." << std::endl;
		}
		else
		{
			std::cout << "There are 0 days in 0 years." << std::endl;
		}
		break;
	case 7:
		system("CLS");
		std::cout << "How many years?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (55*lngVal) << " weeks in " << lngVal << " years." << std::endl;
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (55) << " weeks in 1 year." << std::endl;
		}
		else
		{
			std::cout << "There are 0 weeks in 0 years." << std::endl;
		}
		break;
	case 8:
		system("CLS");
		std::cout << "How many years?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (365*lngVal) << " days in " << lngVal << " years." << std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (365) << " days in 1 year." << std::endl;
		}
		else if (! (std::cin >> lngVal))
		{
			std::cout << "ERROR: Invalid entry!" << std::endl;
		}
		else
		{
			std::cout << "There are 0 days in 0 years." << std::endl;
		}
		break;
	case 9:
		system("CLS");
		std::cout << "How many years?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << (((60*24)*365)*lngVal) << " minutes in " << lngVal << " years." 
				<< std::endl; 
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << ((60*24)*365) << " mintues in 1 year." 
				<< std::endl;
		}
		else
		{
			std::cout << "There are 0 minutes in 0 years." << std::endl;
		}
		break;
	case 10:
		system("CLS");
		std::cout << "How many years?" << std::endl;
		std::cin >> lngVal;
		system("CLS");
		if (lngVal !=0 && lngVal !=1)
		{
			std::cout << "There are " << ((24*365)*lngVal) << " hours in " << lngVal << " years." 
				<< std::endl;
		}
		else if (lngVal == 1)
		{
			std::cout << "There are " << (24*365) << " hours in 1 year." << std::endl;
		}
		else
		{
			std::cout << "There are 0 hours in 0 years." << std::endl;
		}
		break;
	default:
		system("CLS");
		std::cerr << "ERROR: Invalid entry!" << std::endl;
	}

	std::cout << "Enter Y to restart. Closing automatically in 5 seconds..." << std::endl;
	std::cin >> chrRep;

	if (chrRep == 'Y' || chrRep == 'y')
	{
		return main();
	}
	return 0;
}


I am very pleased, this is my 1st useful application :)
Last edited on
Your menu looks like this:


Welcome to the Time Converter, what would you like to convert?
1) Minutes -> Hours
2) Seconds -> Hours
3) Milliseconds -> Hours
4) Seconds -> Minutes
5) Milliseconds -> Minutes
6) Months -> Years
7) Weeks -> Years
8) Days -> Years
9) Minutes -> Years
10) Hours -> Years


However when I select, say, option 8, it asks me 'how many years'. Shouldn't option 8 then say 'Years -> Days' if you're converting from years to days?

Anyway, you're calling main() again from line 227. I can't say that I've ever seen this done before! Ummm...I'm not sure, but I don't really think it's standard practice, so I wouldn't recommend doing that.

You should instead use a do...while loop. That is, "do all the stuff, then get a char asking if they want to restart, and keep doing it while that character isn't a Y/y".

You can use a similar approach to handle validation for the menu selection. Display the menu, and display an error message as long as their input continues to be garbage (i.e. a while loop)...

Hope that helps...
Hey there

Well I made a simple time converter, anyways I am trying to get it to where, lets say the user types in D but the options are only 1-10 I want it to not close and say error. It works if I type in a number not between 1-10 BUT it won't work for anything else


like sammy34 said you should put a loop around it...
Something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {
    /*Do your stuff*/
    while(1) {
        /* Do the stuff you want to have repeated */
        
        std::cout << "Enter Y to restart. Closing automatically in 5 seconds..." << std::endl;

        time_t start_time, cur_time;

        time(&start_time);
        do {
            std::cin >> chrRep;
            if (chrRep == 'Y' || chrRep == 'y') {
                continue;
            }
            time(&cur_time);
         } while((cur_time - start_time) < 5); // 5 seconds wait
    }
    /* Do some more stuff */
    return 0;
}


sleep(int msecs); would just freeze your program

have fun ;-)
Oh and regarding the sleep, you're trying to do two things concurrently (i.e. accept user input and wait a specific amount of time). I'm not sure how best to handle that (another thread?). Anyone care to help us out?
Okay, thanks I fixed #8 btw.

Also the return main(); call just goes back to the main function and repeats. I figured it would take less memory while running the program, dunno though. I'll go back to loops :3

@attaboy: Okay thanks for that, I've never used timers but I guess now's a good time to start :) May I ask though, why do you put a & in-front of the variable?

Is there something I can do that tells me if the user's input is the correct data type using std::cin >> ?

Thanks for your help everyone!

EDIT: What library do I need to have time? ctime?
Last edited on
To check input (it's in here somewhere):
http://www.cplusplus.com/forum/articles/6046/

CometJack wrote:
EDIT: What library do I need to have time? ctime?

What is this referring to?

Also, don't use system for stuff like changing colors. If you REALLY want to change colors and stuff, go learn to use one of the curses libs.
Okay thanks.

Yeah, I added #include <ctime> to do time stuff.

Okay, is there another way I can set the title though? SetConsoleTitle throws errors when I use it :|

Hold on a sec...I'm confused by attaboy's code segment:

1
2
3
4
5
6
7
8
9
10
        time_t start_time, cur_time;

        time(&start_time);
        do {
            std::cin >> chrRep;
            if (chrRep == 'Y' || chrRep == 'y') {
                continue;
            }
            time(&cur_time);
         } while((cur_time - start_time) < 5); // 5 seconds wait 


How will this automatically close the program after 5 seconds? If the user doesn't enter anything, the thread will continue to be blocked by the cin call, right (and hence the 'while' condition will never be evaluated)?
You are absolutely right, thanks for pointing that out sammy34

I haven't been on here for a while. Sorry for that.

I must have simply overlooked that :-(

You might solve the problem for a Windows machine this way (I personally do not have Windows as my operating system):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        time_t start_time, cur_time;

        time(&start_time);
        do {
            if (kbhit()) { // Depends on what OS you're using: Returns 0 if no key is hit, 1 else
                char c = getchar(); // See what key it was
                switch (c) {
                    case 'y':
                    case 'Y':
                        // Code for returning to the top (Might be solved by loop)
                        break;
                    default:
                        break;
                }
            }
            time(&cur_time);
         } while((cur_time - start_time) < 5); // 5 seconds wait  


Again... I'm not working on a Windows machine, so I couldn't test this code segment.

Another option would be multi-thread programming (which seems a bit exaggerated in this case)
Topic archived. No new replies allowed.