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]