c++ struct

Hello... my book that i'm reading wants me to create a program that will declare a structure called Time that contains members to store the hours, minutes, and seconds. I will have to Prompt the user for the input time and then call the GetTime function which will have the job that extracts the integer values and assigns them to the members of a Time Structure. (I KNOW HOW TO DO THIS) but... if GetTime returns a value of false , that means that it wasn't able to get all three integer values. otherwise it should return true... first question is i don't know what to check for for a false to return...

question two: I also have another function called IsTimeVaild which will validate if the user enters a correct time or not . so for example if user enters in "Please enter the time in HH:MM:SS format: 12 24 33" will show invaild display the time required is HH:MM:SS but i don't know how to test to make sure the user enters in the colons.. little lost here.. the GetTime function won't simply wait for the user to enter three separate integer values; instead the input must be in HH:MM:SS format (that's the hours followed by a single colon followed by the minutes followed by another colon followed by the seconds).

question three: AddOneSecond  function this function will add one second to the time. so for example if any of the Time structure fields need to rollover (the hours, minutes or seconds need to be reset to zero) this function will handle that.. .

DisplayTime-----> u will see that my last function should just display the time.


**below is what i have so far.. my strugle is the functions i need to built that's what i'm needing help... this book does not contain the source code so i can't even check what i have any help would be great thanks .

#include    <iostream>
#include    <iomanip>
#include    <cstdlib>
using namespace std;


// structure declaration
//TODO: create the "Time" structure with members for hours, mins and seconds.
struct Time
{
	int hours;
	int mins;
	int seconds;


};


// defined constants
const   int MAX_HOURS = 23;
const   int MAX_MINS = 59;
const   int MAX_SECS = 59;



// function prototypes
void    AddOneSecond(Time  &timeParam);
bool    GetTime(Time  &timeParam);
void    DisplayTime(const Time  &timeParam);
bool    IsTimeValid(const Time  &timeParam);



// ==== main ==================================================================
//
// ============================================================================

int     main(void)
{
    Time       userTime;
	Time       test;
    cout << "Please enter the time in HH:MM:SS format: ";
    if ((false == GetTime(userTime))  ||  (false == IsTimeValid(userTime)))
        {
        cout << "Invalid input..." << endl;
        exit(EXIT_FAILURE);
        }
	
    //display the incremented time
    cout << "The incremented time is ";
     AddOneSecond(userTime);
     DisplayTime(userTime);
     return 0;

}  // end of main

Open in new window

businessesatozAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Infinity08Connect With a Mentor Commented:
>> i understand the if/else statement. i just don't know how to construct my function.

Well, the steps are easy :

1) read the input (the getline function does this)
2) check that reading succeeded (the if does that)
3) check that the format of the string is right (see http:#37016809)
4) get the values out of the string, and into the struct (we'll get to that)

Conbstructing the function, is as easy as doing these 4 steps, one at a time, and adding code as needed.
0
 
Infinity08Commented:
>> first question is i don't know what to check for for a false to return...

That depends entirely on how you decided to get the input. But either way, the idea is to detect any error while trying to read input, and return false if that happens.

Maybe you could show your GetTime function ?


>> i don't know how to test to make sure the user enters in the colons..

You could for example read an entire line of input into a string, and then parse that input to ensure it contains three integer values separated by colons.

That will be difficult to do in a different function from GetTime though, because apparently that's where you get your input. Why not integrate it in the GetTime function ?

I would assume though that the IsTimeValid function is supposed to do something else : ie. check that the three integer values entered correspond to a valid time (eg. no more than 59 seconds).


>> **below is what i have so far.. my strugle is the functions i need to built that's what i'm needing help...

I recommend you just give it a try, and post back here how far you get with that.


>> this book does not contain the source code so i can't even check what i have any help would be great thanks .

You can use a compiler though, to see the output and verify that it's correct.
0
 
businessesatozAuthor Commented:
the IsTimeValid function will check to make sure that time is vaild.. i think GetTime function is already doing this..but that's okay i guess i can write it twise.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
businessesatozAuthor Commented:
ALso i'm very confused on the GetTime function.. can u post me some kind of template i can work off because i have no idea what to even start off with..  i know how to get the times but just don't know how to make sure the user types in the colon...

bool    GetTime(Time  &timeParam)
{
 
	cin>>timeParam.hours;
	cin>>timeParam.mins;
	cin>>timeParam.seconds;

}

Open in new window

0
 
Infinity08Commented:
>>  i know how to get the times but just don't know how to make sure the user types in the colon...

As I said : try reading an entire line of input into a string, and then parse that string.

Have a look at the getline function eg. :

        http://www.cplusplus.com/reference/string/getline/
0
 
Hugh McCurdyCommented:
Infinity, I'm reading the question differently than you.  I think the author is asking two questions.  (1) How to get the string, which you answered.  (2) How to parse the string ("but just don't know how to make sure the user types in the colon").  This might answer the second question.

http://www.cplusplus.com/reference/clibrary/cstring/strtok/
0
 
Infinity08Commented:
I'm not reading it differently ;) I'm just taking it one step at a time.
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
	string mytime;
	timeParam.hours;
	getline (cin,mytime);


}

Open in new window


would that be correct...? any way you can provide me the GetTime function so i can understand thanks.. i really understand via examples.
0
 
Hugh McCurdyCommented:
Infinity.  Understood.  
businessesatoz (please don't give me any points for my comment; my advice is you let infinity guide you.)
0
 
Infinity08Commented:
>> getline (cin,mytime);

Ok, so that gives you the input in a string.

Now, notice how the getline function returns the stream ?
Notice also that a stream can easily be checked for errors by converting it to a bool. So, something like this will check if the reading succeeded :
if (getline(...)) {
    // OK : reading succesful
}
else {
    // Oops : something bad happened
}

Open in new window

0
 
businessesatozAuthor Commented:
thanks..i'm so lost.
0
 
businessesatozAuthor Commented:
Infinity08: i will try your latest post thanks :) you guys rock i will try and post.
0
 
businessesatozAuthor Commented:
but the

	string mytime;
	
	getline(cin,mytime);
	

Open in new window

looks like it's not taking any input.. how do i give it my input of hours, minutes and seconds from my function?

bool    GetTime(Time  &timeParam)
{
 
	string mytime;
	
	getline(cin,mytime);
	

}

Open in new window

0
 
Infinity08Commented:
Then, once you are sure you have the string, you can then start reading it.

A time has a very specific format. For now, let's stick to the most restrictive format to keep it easy (HH:MM:SS). So you'll need 8 characters : the first two need to be digits, then a colon, then two more digits, another colon, and another two digits.

For example, you can check each character in the string one at a time. If that's the approach you choose, the isdigit function will probably be of interest :

        http://www.cplusplus.com/reference/clibrary/cctype/isdigit/
0
 
Infinity08Commented:
>> looks like it's not taking any input.. how do i give it my input of hours, minutes and seconds from my function?

If you enter the time, followed by the RETURN key, the time will be placed in the string.

You then still need to parse that string to get the three separate values out of it, and place them in the struct.

But first let's validate the format of the string - see my previous post.
0
 
businessesatozAuthor Commented:
i understand the if/else statement. i just don't know how to construct my function. can you provide me a worked out example i can look at peace by peace please thanks Infinity08 (sorry for the trouble)
0
 
businessesatozAuthor Commented:
i understand what your saying but i dont know how to write the syntax correctly, i'm asking if you can please place the syntax here so i can see how it has been done as i have to do the other functions and so i can leran.. i really by seeing code and then trying it myself. thanks :)
0
 
Infinity08Commented:
I won't give you the code no - that is not allowed.

However, if you give it a try, I'm sure you'll be able to do it. You might have to do some research on how the functions work (that's why I added the reference links), and you might have to ask clarifying questions, but that's all necessary to learn programming.

No shortcuts ;)
0
 
businessesatozAuthor Commented:
not allowed for what... ? i'm just practing a book problem.
0
 
businessesatozAuthor Commented:
you don't need to provide me code but can you provide me the template so i can fill in the code... i really learn by example and would like to see what i'm doing wrong i been trying everything and i'm stuck.
0
 
Infinity08Commented:
See http:#37016832 for the template ;)

And the posts before that for further details on each of the steps.
0
 
Infinity08Commented:
You can just say if you don't want my help ... No need to open a new question (http://www.experts-exchange.com/Programming/Languages/CPP/Q_27411675.html), to avoid me.
0
 
businessesatozAuthor Commented:
check that reading succeeded (the if does that) ---> not sure what i'm checking for the getline function should just read whatever the user types in..
0
 
businessesatozAuthor Commented:
i do want your help. i'm just pissed off and confused that i can't get this going.so i thought i break my question down into a simpler question then display my entire code to see what i have done so far.
0
 
businessesatozAuthor Commented:
so Infinity08, what am i check for ?
string MyTime;
	getline(cin,MyTime);

Open in new window

I get this code but this would already read anything the user types so why would i need to have an if/else statement to check if reading has succeeded ?
0
 
Infinity08Commented:
Right, well I was willing to assist you with this whole exercise - all in one (this) question. But I can't help you if you'll be working with different people spread out over different questions, trying to work on the same thing. That's counter-productive.

There's also no reason to be pissed off. Programming is all about overcoming obstacles, and finding ways to do so, by learning new things.

One of the important things to learn, is to break up a problem in smaller/easier sub-problems (like I illustrated), and then tackling each of these in turn (like I suggested). It will require some effort, but again : that's part of being a programmer.


>> check that reading succeeded (the if does that) ---> not sure what i'm checking for the getline function should just read whatever the user types in..

Yes, but what if it failed to read anything for whatever reason (like an invalid input stream eg.) ?

There are of course more checks to do, most of which are done in step 3) detailed above.
0
 
Infinity08Commented:
Btw, I recommend deleting the other question, in order to avoid confusion.
0
 
businessesatozAuthor Commented:
okay sorry about that i thought it would be helpful..
0
 
businessesatozAuthor Commented:
i'm now stuck at how to check if the string is in proper format..
after getting the entire string "HH:MM:SS" not sure how to check if in proper format.

http://www.cplusplus.com/reference/clibrary/cctype/isdigit/ shows checking an array position[0] but in my case i have a string how can i check it's position 0 or any position.. so i can check character by character to make sure a : was typed in.
0
 
Infinity08Commented:
The std::string type has the operator[] overloaded :

        http://www.cplusplus.com/reference/string/string/operator%5B%5D/

so you can access the character at position 0 using str[0].
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	getline(cin,MyTime);

	if(isdigit(MyTime[0]))
	{
		cin >> timeParam.hours;
	}
	else if(isdigit(MyTime[1]))
	{
		cin >> timeParam.hours;
	}
	
	return true;
}

Open in new window

 so something like this work ...?
0
 
Infinity08Commented:
You have skipped step 2).

And the implementation for step 3) looks a bit strange. For a few reasons :

(a) you're reading more input, but you shouldn't, because you've already read the input.
(b) you're only trying to check the two first characters, not the other 6.
(c) you're only checking the second character if the first is NOT a digit. You'll have to check all 8 characters, and only fail if one doesn't match the expected format.
(d) you're always returning true - the point of checking, is to spot errors, and return false in case of an error.
0
 
sarabandeConnect With a Mentor Commented:
note, you can turn a digit to a number by subtracting '0'. the result of such a subtraction is a char what also is a signed integer (normally 8-bit in the range from -128 to 127).

if you have turned both digits of - say hour - to numbers you would get the total hour as integer by multiplying the first number by 10 and add the second number.

Sara
0
 
businessesatozAuthor Commented:
Hey sara, my goal is to design a function which allows the input of HH:MM:SS so it allows "HH:MM:SS" in that exact format with the colons.
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	
	getline(cin,MyTime);

	if(isdigit(MyTime[0]))
	{
		timeParam.hours >> MyTime[0];
	}
	else if(isdigit(MyTime[1]))
	{
		timeParam.hours >> MyTime[1];
	}
	else if(isdigit(MyTime[2]))
	{

	}
	
}

Open in new window


what do i do ones  i get to a character which is not a digit... because i want the time format to be in HH:MM:SS thanks.
0
 
Infinity08Commented:
>> what do i do ones  i get to a character which is not a digit...

If you expect a digit, but don't get one, then that's an error, so you return false.

But, looking at your code, I think you need to do a bit more reading up on the earlier chapters of the book, namely about how if statements work, and what the stream operator (>>) does.

Next, take it very slowly - one step at a time. Don't try to store anything in the struct yet. Just try to validate that the format is correct. So, check each character in the string, whether it's as expected.

Only when all characters are as expected, you can start worrying about getting the values out.
0
 
businessesatozAuthor Commented:
Thanks, i understand that i need to return false.. but what i mean is i want to get a colon now becuase so far i'm thinking i got HH and now i'm at my first colon. so how should i make sure the user typed in the colon.. should I have a char colon in the struct to take in the colon?
0
 
businessesatozAuthor Commented:
so then it would read something like this ....

bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	
	getline(cin,MyTime);

	if(isdigit(MyTime[0]))
	{
		timeParam.hours >> MyTime[0];
	}
	else if(isdigit(MyTime[1]))
	{
		timeParam.hours >> MyTime[1];
	}
	else if(isdigit(MyTime[2]))
	{
		timeParam.colon >> MyTime[2];
	}
	
}

Open in new window

0
 
Infinity08Commented:
As I said : forget about the struct for now.

Just validate each of the characters in the string.
0
 
businessesatozAuthor Commented:
or maybe version two.

bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	
	getline(cin,MyTime);

	if(isdigit(MyTime[0]))
	{
		timeParam.hours >> MyTime[0];
	}
	else if(isdigit(MyTime[1]))
	{
		timeParam.hours >> MyTime[1];
	}
	else if(MyTime[2]==':')
	{
		timeParam.colon >> MyTime[2];
	}
	
}

Open in new window

0
 
Infinity08Commented:
The comparison with the colon is better that way.

However, the rest isn't. Please read up on if statements, and the stream operator, as I suggested before.
0
 
sarabandeCommented:
yes, i know, but you should have better read what infinity08 has told you.

for example that you should handle the case when the condition was wrong and not when it was ok. so check for  

     
(! isdigit(MyTime[x]))

Open in new window


where  x  is from those indexes where you expect a digit and do a cout statement that a non-digit was entered and return with false.

also  the statement 'else if(isdigit(MyTime[2]))' doesn't make so much sense as MyTime[2] should be a colon and not a digit. so check whether MyTime[2] is not equal to ':' and also give a message and return false if you find some other character there.

then  the 'timeParam.hours >> MyTime[0];' is not a senseful statement for you, cause the left operand is an integer and not an input  stream (like for example in cin>>i). you want to fill the timeParam.hours and not take it as source. so you need to apply a formula like   h = n1 * 10 + n2  to calculate the hours. n1 is the number you need to calculate from first digit. n2 is the number you calculate from second digit and i already told you how to calculate the number from a digit, for example the number 5 is '5'-'0'.

Sara

0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	
	getline(cin,MyTime);


	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else
	{
		return true;
	}
	
}

Open in new window


thanks .. does something like this make sense..?
0
 
Infinity08Commented:
I'm sure you can answer that for yourself ?

What does that code do ?

Is that what you want it to do ?
0
 
businessesatozAuthor Commented:
well the goal is to check if the first typed in value is a digit, otherwise if it's not then return false. otherwise return true.  now if it's true i just need to read into the hours struct
0
 
Infinity08Commented:
>> otherwise return true.

If you return true, you end the function. Is that what you want ?


>> now if it's true i just need to read into the hours struct

As I said before : forget about the struct for now. Just focus on validating the input.
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
      string MyTime;
      
      getline(cin,MyTime);


      if(!isdigit(MyTime[0]))
      {
            return false;
      }
      else if(!isdigit(MyTime[1]))
      {
            return false;
      }
      else if(MyTime[2]!=':')
      {
            return false;
      }
      else if(!isdigit(MyTime[3]))
      {
            return false;
      }
      else if(!isdigit(MyTime[4]))
      {
            return false;
      }
      else if(MyTime[5]!=':')
      {
            return false;
      }
      else if(!isdigit(MyTime[6]))
      {
            return false;
      }
      else  if(!isdigit(MyTime[7]))
      {
            return false;
      }
      else
      {
            
            return true;
      }

}
alright ... i think i finally got it..
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
	string MyTime;
	
	getline(cin,MyTime);


	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		
		return true;
	}

}

Open in new window



**forgot the "code" tags sorry..
0
 
businessesatozAuthor Commented:
p.s don't u sleep Infinity08: lol?
0
 
Infinity08Commented:
>> alright ... i think i finally got it..

That validates the input properly. Good !

You still need step 2) I described above though - make sure that the getline actually succeeded.
0
 
businessesatozAuthor Commented:
I think now i'm at the 4) get the values out of the string, and into the struct (we'll get to that)
0
 
businessesatozAuthor Commented:
i noticed that their is a cin.fail should i be using that to check if it fails or not?
0
 
Infinity08Commented:
>> i noticed that their is a cin.fail should i be using that to check if it fails or not?

Please have a look at http:#37016783
0
 
businessesatozAuthor Commented:
Hey , i revisited that link, not sure what i'm checking for .. i can't seem to think how this would fail now.even then if(getline!=cin.fail) is incorrect syntax.
0
 
Infinity08Commented:
As I've said a few times before - you need to make sure that the getline actually succeeded in reading the data. If you don't check that, then you can't be sure you're even validating real data.

So, check the result of getline.
0
 
businessesatozAuthor Commented:
thanks.. i have rewrote the function

bool    GetTime(Time  &timeParam)
{
 
	char MyTime[9];
	
	if(!cin.fail())
	{
		cin.getline(MyTime,9);
	}
	else
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		
		return true;
	}
}

Open in new window

0
 
Infinity08Commented:
I'm a bit mystified why you didn't just use the code as I showed it.

The approach as you have it at this moment, now has another problem that we previously didn't have.

Now you need to do damage control in case the buffer is filled by inputting more than 8 characters.

That's not a good situation.
0
 
businessesatozAuthor Commented:
oh shit your right (of course) here is an updated version
bool    GetTime(Time  &timeParam)
{
 
	string  MyTime;
	
	getline(cin,MyTime);

	if (!getline(cin,MyTime))
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		
		return true;
	}
}

Open in new window

0
 
Infinity08Commented:
How many lines of input are you now reading ?
0
 
Hugh McCurdyCommented:
Just chiming in -- no points please -- Infinity makes an important point about preventing buffer overflow.  

People using classic C are stuck using char[] (afaik).  You don't have to be stuck like that in C++.  Use String like Infinity08 recommends.  Please.
0
 
businessesatozAuthor Commented:
Yeah i'm trying too.. but i don't understand how to make sure that we did get a good read using the getline function their is no getline fail i can check
0
 
businessesatozAuthor Commented:
Hello Infinity08, can you show me how to pass the values to my struct while i pounder about the getline check .. as i'm stuck with that. if the string match HH:MM:SS then the struct needs to get the values from the string. in the last else statement in my code which means everything is good
0
 
Infinity08Commented:
>> Hello Infinity08, can you show me how to pass the values to my struct while i pounder about the getline check ..

In the last code you posted, you're correctly checking that the second getline succeeded.

But what's the first getline doing there ?


>> if the string match HH:MM:SS then the struct needs to get the values from the string.

Correct.


>> in the last else statement in my code which means everything is good

And correct again.
0
 
businessesatozAuthor Commented:
alright , so now i have it as follows.

bool    GetTime(Time  &timeParam)
{
 
	string  MyTime;
	

	if (!getline(cin,MyTime))
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		
		return true;
	}
}

Open in new window

0
 
Infinity08Commented:
Looking good.

So, now you have different options for getting the values out of the string, and into the struct.

The one that will probably teach you most, is arguably also the most straightforward.

Let's focus on the hours value, which consists of two digits in MyTime[0] and MyTime[1] respectively.

How can you convert these two characters into an int value ?

First take one character, which we know is a digit, so it's somewhere between '0' and '9'. If you subtract '0' from that character, you get an integer value between 0 and 9. You can convert both characters to ints that way, and then these two ints can be combined into one using simple arithmetic.

Have a go at it.
0
 
businessesatozAuthor Commented:
bool    IsTimeValid(const Time  &timeParam)
{
	return true;

}

bool    GetTime(Time  &timeParam)
{
 
	string  MyTime;
	

	if (!getline(cin,MyTime))
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		int hour;
		
		hour = (MyTime[0]-0 + MyTime[1]-0);
		timeParam.hours=hour;
		return true;
	}
}

Open in new window

see if this code makes you laugh ..
0
 
businessesatozAuthor Commented:
else
	{
		int hour;
		
		hour = (MyTime[0]-'0' + MyTime[1]-'0');
		timeParam.hours=hour;
		return true;
	}
}

Open in new window

I think maybe i have to substract like this '0' ?
0
 
businessesatozAuthor Commented:
Okay i got the hours (i believe) working okay.. please look at the end of the code.

bool    GetTime(Time  &timeParam)
{
 
	string  MyTime;
	

	if (!getline(cin,MyTime))
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		int hour;
		
		hour = (10* (MyTime[0]-'0') + MyTime[1]-'0');
		timeParam.hours=hour;
		return true;
	}

Open in new window

0
 
Hugh McCurdyCommented:
Again, no points please.  Looks quite good.  My personal preference would be add parentheses to guarantee execution order and to have better self documentation.

		hour = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

Open in new window

0
 
businessesatozAuthor Commented:
But when someone puts in 00 it only displays 0 without the other 0 ...?is that okay .. ?
0
 
businessesatozAuthor Commented:
how do i show the minutes?  
minutes= (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

Open in new window

would that still work?
0
 
businessesatozAuthor Commented:
I'm sorry I mean

minutes= (10 * ( MyTime[3]-'0') + ( MyTime[4]-'0' ));

Open in new window

0
 
Hugh McCurdyConnect With a Mentor Commented:
It is OK for now.  This formatting problem can be solved. How are you trying to output it?  cout?  printf?  If using printf, it's something like

printf ( "hour = %02d\n", hour );

Open in new window


I'm sure there's a way to do this with cout too but I'd have to look it up and I doubt Infinity has to look it up.

For now, while you are waiting for Infinity, I suggest you relax with the knowledge that the formatting issue can be solved.  In the meantime, I suggest you solve the minutes and seconds problem (if you haven't already).
0
 
businessesatozAuthor Commented:
bool    GetTime(Time  &timeParam)
{
 
      string  MyTime;
      

      if (!getline(cin,MyTime))
      {
            return false;
      }
      
      if(!isdigit(MyTime[0]))
      {
            return false;
      }
      else if(!isdigit(MyTime[1]))
      {
            return false;
      }
      else if(MyTime[2]!=':')
      {
            return false;
      }
      else if(!isdigit(MyTime[3]))
      {
            return false;
      }
      else if(!isdigit(MyTime[4]))
      {
            return false;
      }
      else if(MyTime[5]!=':')
      {
            return false;
      }
      else if(!isdigit(MyTime[6]))
      {
            return false;
      }
      else  if(!isdigit(MyTime[7]))
      {
            return false;
      }
      else
      {
            int hour;
          int minutes;
            int seconds;

            hour = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));
      
            minutes=(10 * ( MyTime[3]-'0') + ( MyTime[4]-'0' ));

            seconds = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

            timeParam.hours=hour;
            timeParam.mins=minutes;
            timeParam.seconds=seconds;
            return true;
      }
}
0
 
businessesatozAuthor Commented:
thanks for your help so far.. I believe i have solved this correctly for minutes and seconds using the same formula.

bool    GetTime(Time  &timeParam)
{
 
	string  MyTime;
	

	if (!getline(cin,MyTime))
	{
		return false;
	}
	
	if(!isdigit(MyTime[0]))
	{
		return false;
	}
	else if(!isdigit(MyTime[1]))
	{
		return false;
	}
	else if(MyTime[2]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[3]))
	{
		return false;
	}
	else if(!isdigit(MyTime[4]))
	{
		return false;
	}
	else if(MyTime[5]!=':')
	{
		return false;
	}
	else if(!isdigit(MyTime[6]))
	{
		return false;
	}
	else  if(!isdigit(MyTime[7]))
	{
		return false;
	}
	else
	{
		int hour;
	    int minutes;
		int seconds;

		hour = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));
	
		minutes=(10 * ( MyTime[3]-'0') + ( MyTime[4]-'0' ));

		seconds = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

		timeParam.hours=hour;
		timeParam.mins=minutes;
		timeParam.seconds=seconds;
		return true;
	}
}

Open in new window

0
 
Hugh McCurdyCommented:
Yes on the minutes question.  (The second post)
0
 
Hugh McCurdyCommented:
Fix indices in the second calculation line.  Otherwise looks good.
0
 
businessesatozAuthor Commented:
alright thanks... u want 1 point at least lol or u going to beg me not too give u any points? j/k
0
 
businessesatozAuthor Commented:
alright i managed to complete the other function myself and looks like it's working after i tested can you confirm that i also looks right? (kind of glad this book did not provide the answers makes u work way harder to think about solutions)

bool    IsTimeValid(const Time  &timeParam)
{
	if((timeParam.hours > 24) || timeParam.hours < 0)
	{
		return false;
	}
	else
	{
		return true;
	}

}

Open in new window


0
 
Hugh McCurdyCommented:
Infinity did an awful lot of work and should get credit for that.  I suppose he could live with an A and 499 points (would be kinda funny if it happened to me but I don't know how he feels).  

I'd change line 3 to (but it's what I would do).

	if((timeParam.hours > 24) || ( timeParam.hours < 0))

Open in new window


I would also test that hour, minute and second were valid before you set the value.  Instead of

		hour = (10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

Open in new window

I would have
setHour ( 10 * ( MyTime[0]-'0') + ( MyTime[1]-'0' ));

Open in new window


Your setHour() function would test that the value is in range and if it was set the hour and return true.  Otherwise it would not change the hour and return false.

0
 
businessesatozAuthor Commented:
alright i see what your saying. sounds good . :)
0
 
businessesatozAuthor Commented:
My last but not least function i want to get working now is

my AddOneSecond function.. This function will add one second to the time -- if any of the Time structure fields need to "rollover" (e.g., the hours, minutes or seconds need to be reset to zero), this function will handle that.

this is what i have so far.. if you can guide me in the right directions that would be good.

void    AddOneSecond(Time  &timeParam)
{
	
	
	timeParam.seconds=timeParam.seconds+1;

}

Open in new window

But this code is not adding 1 second as a matter of fact i don't think it's doing anything.
0
 
businessesatozAuthor Commented:
any help on this guys/gals...
0
 
businessesatozAuthor Commented:
Thank you for all your help... goingt to post a new question in new message regarding another function.
0
 
sarabandeCommented:
to make it perfect :) you should add a length check on the input, above all other checks. length should be 8 or expressions like MyTime[7] would fail.

the printf hmccurdy showed, would be made in c++ like

std::cout << std::setw(2) << std::setfill('0') << std::right << hour;

Open in new window


you need to include <iomanip> for that. the setw limits next output to 2 chars, the setfill determines the fill character, the right makes next output right justified.

Sara
0
 
businessesatozAuthor Commented:
Thanks sara.. posted another question regarding the same question from the same book i'm learning from. you guys have been very helpful thank you..
0
 
Infinity08Commented:
I'm finally back, after some much needed sleep ;)

I see you've made some good progress with the help of hmccurdy. Great !


One last thing about the AddOneSecond function : the code you posted adds 1 second to the seconds value, but you need to account for the case where you increment the seconds from 59 to 60. In that case, the seconds need to be reset to 0, and the minutes need to be incremented by 1. Note that the same is true for the minutes ! And for the hours !
0
 
sarabandeCommented:
0
 
businessesatozAuthor Commented:
Infinity08:  i really thank you for your help.. and thanks for all the help and being patient man.. good karma will come to you not that you need it ... its always good karma or good money you need but anyhow you been awsome and so has everyone on this board.. i have learned alot and i feel much better about my understanding . i plan to start my next chapter and i'm sure i will be lost at some point again and will post to get a better understanding.
0
 
Infinity08Commented:
I'm just glad I could help out :)
0
All Courses

From novice to tech pro — start learning today.