converting 12hr to 24hr with exception handling

Okay, it is compiling alright. But when I enter the time, it says that I entered an invalid value, regardless of what number I enter. And it just keeps asking for a new value (like it should, but when it shouldn't). I think I have my code right (I'm sure it is riddled with small errors, maybe large ones). Does anyone see why it is doing that though?


Here is my exact assignment in case you think I am going about it wrong:

Write a program that prompts the user to enter time in 12-hour notation. The program then outputs the time in 24-hour notation. Your program must contain three exception classes: invalidHr, invalidMin, and invalidSec. If the user enters an invalid value for hours, then the program should throw and catch an invalidHr object. Similar conventions for the invalid values of minutes and seconds.



Here is all of my 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>
#include "ConvertTimeHeader.h"

using namespace std;

int main()
{
	convertTime convert;
	int hour, min, sec;
	
	cout << "Please input time in 12 hr notation seperated by spaces: ";
	cin >> hour >> min >> sec;
	
	convert.invalidHr(hour);
	convert.invalidMin(min);
	convert.invalidSec(sec);
	convert.printMilTime();
	
	system("Pause");
	
	return 0;	
	
}




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
#include <iostream>
#include "ConvertTimeHeader.h"
using namespace std;

void convertTime::invalidHr (int hour)
{
	try{
		if (hour > 0 && hour < 13)
			hour = hour + 12;
		else
			cin.clear();
			cin.ignore();
			cout << "Ivalid input! Please input hour again in correct 12 hour format: ";
			cin >> hour;
			invalidHr(hour);
			
	}
	catch (...) { cout << "Invalid hour input!";}
}

void convertTime::invalidMin (int min)
{
	try{
		if (min < 60 && min > 0)
			min = min;
		else
			cin.clear();
			cin.ignore();
			cout << "Please input minutes again in correct 12 hour format: ";
			cin >> min;
			invalidMin(min);
			
	}
	catch (...) { cout << "Invalid minute input!";}
}

void convertTime::invalidSec(int sec)
{
	try{
		if (sec < 60 && sec > 0)
			sec = sec;
		else
			cin.clear();
			cin.ignore();
			cout << "Please input seconds again in correct 12 hour format: ";
			cin >> sec;
			invalidSec(sec);
			
	}
	catch (...) { cout << "Invalid second input!";}
}

void convertTime::printMilTime()
{
	cout << "Your time converted: " << hour << ":" << min << ":" << sec;
}



[code]
class convertTime
{

public:
int hour, min, sec;

void invalidHr(int hour);
void invalidMin(int min);
void invalidSec(int sec);
void printMilTime();

};

[code]
You need to put curly braces around your else statements:
8
9
10
11
12
13
14
15
16
17
if (hour > 0 && hour < 13)
    hour = hour + 12;
else
{
    cin.clear();
    cin.ignore();
    cout << "Ivalid input! Please input hour again in correct 12 hour format: ";
    cin >> hour;
    invalidHr(hour);
}

Otherwise, only the first statement will be part of the else and everything else will be outside of it.
Ahhhhhh. That was it! Now i just need to figure out why it is giving incorrect values as output...
Okay, I give up on this issue too. It runs and does what it's supposed to when it's supposed to...except it only outputs garbage. However, it is consistent garbage. Each time I compile and run it, it spits out the same bad numbers. It's always "332:8727496:83" Which obviously isn't correct. This is the minor change I have done in the code (nothing changed for the header). Am I missing something else? Thanks!

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
#include <iostream>
#include "ConvertTimeHeader.h"

using namespace std;

int main()
{
	convertTime convert;
	int hr, mn, sc = 0;
	
	cout << "Please input hours in 12 hr notation: ";
	cin >> hr;
	cout << "Please input minutes: ";
	cin >> mn;
	cout << "Please input seconds: ";
	cin >> sc;
	
	convert.invalidHr(hr);
	convert.invalidMin(mn);
	convert.invalidSec(sc);
	convert.printMilTime();
	
	system("Pause");
	
	return 0;	
	
}



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
#include <iostream>
#include "ConvertTimeHeader.h"
using namespace std;

int convertTime::invalidHr (int hour)
{
	try{
		if (hour < 13 && hour > 0)
			{hour = hour + 12;
			return hour;}
		else{
		
			cin.clear();
			cin.ignore();
			cout << "Invalid input! Please input hour again in correct 12 hour format: ";
			cin >> hour;
			invalidHr(hour);
			throw 10;
		}
			
	}
	catch (int c) { cout << "Invalid hour input!";}
}

int convertTime::invalidMin (int min)
{
	try{
		if (min < 60 && min > 0)
			{return min;}
		else{
		
			cin.clear();
			cin.ignore();
			cout << "Invalid input! Please input minutes again in correct 12 hour format: ";
			cin >> min;
			invalidMin(min);
			throw 20;
			return 0;
		}
			
	}
	catch (int e) { cout << "Invalid minute input!" << endl;}
}

int convertTime::invalidSec(int sec)
{
	try{
		if (sec < 60 && sec > 0)
			{return sec;}
		else{
		
			cin.clear();
			cin.ignore();
			cout << "Invalid input! Please input seconds again in correct 12 hour format: ";
			cin >> sec;
			invalidSec(sec);
			throw 30;
			return 0;
		}
			
	}
	catch (int t) { cout << "Invalid second input!" << endl;}
}

void convertTime::printMilTime()
{
	cout << "Your time converted: " << hour << ":" << min << ":" << sec;
}
Never mind. I figured it out. I wasn't setting the variables I was passing to the function equal to a new variable in the function before I allowed it to be messed with. For instance:

1
2
3
convertHour = hour;
convertMin = min;
convertSec = sec;


Each in its respective function and adding the protected variables in the header. Thanks for the input on the earlier problem!
Topic archived. No new replies allowed.