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
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)...
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
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;
}
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?
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 >> ?
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)