get hours, minutes, test in function, can't figure out clean exit on error

I'm trying to test for valid hours and minutes, and exit void getInput(int hours, int minutes) cleanly without going on to the next step as if they were valid. Sorry if this is not clear, what I am going round and round with is when I have an invalid input ,I want to continue the loop and ask for another time, without printing the output as if it were valid. What I have tried either exits without asking if user wants to exit(required) or reports the input is invalid and then couts the time as if it were valid. I am really confused. I think my problem may be with the way I am doing the bool test in this line: if ((!validhours) || (!validmins))
Any help is appreciated

#include <iostream>

using namespace std;

//function definitions
void getInput(int hours, int minutes);

int main()
{
char cont = 'y';
int hours, minutes, hours12;
cout<< "Timer\n";
//call functions
getInput(hours, minutes);
do
{
cout << "Would you like to enter another time? \n";
cin >> cont;
}
while (cont== 'y'|| cont =='Y');
cout << "Thank you.\n";
return 0;
}


// get input
void getInput(int hours, int minutes){
int hours12, mins;
char niteday;
cout << "Please enter hours: ";
cin >> hours12;
cout << "Enter 'A' for am or 'P' for pm ";
cin >> niteday;
cout << "Please enter minutes: ";
cin >> mins;
bool validhours = (hours12 < 1 || hours12 >12);
if (validhours){
cout << hours12 << " is not a valid hour." << endl;
//return 0;
}
bool validmins = (mins < 1 || mins > 60);
if (validmins){
cout << minutes << " are not valid minutes." << endl;
//return 0;
if ((!validhours) || (!validmins))
cout << "Current time is " << hours12 << ":" << minutes << endl;
}
}
Last edited on
To give you a quick idea

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
#include <iostream>

using namespace std;

//function definitions
void getInput(int &hours, int &minutes, char& am_pm); //Pass by reference functions

int main()
{
    char cont = 'y';
    char am_pm = 'A';
    int hours, minutes, hours12;
    cout << "Timer\n";
    //call functions

    do
    {
	   getInput(hours, minutes, am_pm);

	   cout << "Would you like to enter another time? \n";
	   cin >> cont;
    } while (cont == 'y' || cont == 'Y');
    cout << "Thank you.\n";
    return 0;
}


// get input
void getInput(int &hrs, int &mins, char &niteday){
    //int hours12, mins;
    //char niteday;
    cout << "Please enter hours: ";
    //cin >> hours12;
    cin >> hrs;

    // Validate the user input to ensure it is correct input.
    /**************************************************/
    while (hrs < 1 || hrs > 12)
    {
	   cout << hrs << " is not a valid hour.\n";
	   cout << "Please enter hours: ";
	   cin >> hrs;
    }
    /**************************************************/


    cout << "Enter 'A' for am or 'P' for pm ";
    cin >> niteday;
    cout << "Please enter minutes: ";
    cin >> mins;


    // Validate the user input to ensure it is correct input.
    /**************************************************/
    while (mins < 0 || mins > 59)
    {
	   cout << mins << " are not valid minutes.\n";
	   cout << "Please enter minutes: ";
	   cin >> mins;
    }
    /**************************************************/

    cout << "The time you inputted is: " << hrs << ":" << mins <<
	   " " << niteday << endl;

    //bool validhours = (hours12 < 1 || hours12 >12);
    //if (validhours){
	   //cout << hours12 << " is not a valid hour." << endl;
	   ////return 0;
   // }
    //bool validmins = (mins < 1 || mins > 60);
    //if (validmins){
	   //cout << minutes << " are not valid minutes." << endl;
	   ////return 0;
	   //if ((!validhours) || (!validmins))
		  //cout << "Current time is " << hours12 << ":" << minutes << endl;
   // }


}
Last edited on
Topic archived. No new replies allowed.