daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Look at the time input validation stuff and try to adopt it for complex number input (and other user types):
struct Time {
int hh, mm;
std::string ampm; // "AM" or "PM"
};
bool getTime(std::istream& is, Time& t)
{
bool res = false;
char c;
if (is >> t.hh) { // true if it's a number
if (is.get(c) && c == ':' && (is >> t.mm)) { // :MM here
if ((is >> t.ampm) && (t.ampm == "AM" || t.ampm == "PM")) {
// Check up hh & mm values
if (t.hh >= 0 && t.hh <= 12 && t.mm >= 0 && t.mm < 60)
res = true; // good time value
}
}
}
return res;
}
// Advanced (syntax sugar):
std::istream& operator>>(std::istream& is, Time& t) {
if (!getTime(is,t))
is.setstate(std::ios::failbit);
return is;
}
std::ostream& operator<<(std::ostream& os, const Time& t) {
os << t.hh << ':' << t.mm << ' ' << t.ampm;
return os;
}
However this approach has a serious defect in the console input environment. If the user types a part of a valid time value only then getTime waits the rest of Time value. It seems like you program hangs. Possible workaround: read a line then use istringstream:
bool GetTime(Time& t)
{
bool res = false;
std::string line;
if (std::getline(std::cin,line)) {
std::istringstream is(line);
res = getTime(is,t);
}
return res;
}
Of course, it's not areference implementation. Sorry but I have no time to prepare Program Logic Manual for this improvisation ;)...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>btw, i never used stringstreams, and i dont even know what is iss
Yeah well, you learn something new tous le jour and such and such.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Well personally I'd use a vector:
vissssssssssssssssssss
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int main()
{
vector <string> array;
string tmp = "12:20";
string token;
istringstream iss(tmp);
while ( getline(iss, token, ':') )
{
array.push_back(token);
}
cout <<array[0] <<endl;
cout <<array[1] <<endl;
cin.get();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I guess, but I'm just conjecturing here.
I was thinking you might get into trouble with times such as "03:45" but maybe stringstream converts the "03" to 3 anyhoo.
Don't quote me on that.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Is that a joke? No the bit where you're not quite getting the istringstream?
Post an example of what you've tried, state what you expect, and what you're unsure of.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439