Help on Amazing Clock

Hello guys, I did the first part of creating a amazing clock, that allows user to input the time. And whatever time they input they get an event or not. Here is what I have so far.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

const char colon = ':';
int hours;
int minutes;
void getTime(int &, int &);
bool Valid(int, int);

int main(int argc, char *argv[]) {
int hr, min;

cout << "Time format: <HH>:<MM>" << endl;
while (true)
{
getTime(hr, min);

}
return 0;
}

void getTime(int &h, int &m) {
string in;
char c;


do {
cout << "Enter time (HH:MM) : ";


}

while (!(cin >> h >> c >> m) && (c != colon) || Valid == false);

{
cout << "Oops! No value was entered!" << endl << endl;
cin.clear();
fflush(stdin);


}
}

bool Valid(int h, int m)
{
return (h > 0) && (h < 12) && (m > 0) && (m < 59);
}


I been getting some errors, and I cannot format it where you have to input in one line (ex. 12:11) and any other input in the 12 hour format is invalid, like negative time, -1: , : , 1:1, and stuff like this. Can anyone help me?
Last edited on
Anyone? please help
User input is hard.

Read an entire line into a string. Parse the string.
Don't fflush(stdin).

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
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;


bool is_valid( int h, int m )
{
  return (h > 0) && (h <= 12) && (m > 0) && (m <= 59);
}


bool get_time( int &hours, int &minutes )
{
  cout << "What time is it (HH:MM)? ";
  string s;
  getline( cin, s );

  istringstream ss( s );
  char c;
  ss >> hours >> c >> minutes;

  return ss.eof() && (c == ':') && is_valid( hours, minutes );
}


int main()
{
  int hours, minutes;

  while (!get_time( hours, minutes ))
  {
    cout << "Try again.\n";
  }

  cout << "Good job! The time you entered was " << hours << ":" << setw( 2 ) << setfill( '0' ) << minutes << endl;

  return 0;
}

Hope this helps.
Ah yes thank you, forgot to read the entire string, i was more focus on each input.
Topic archived. No new replies allowed.