Link to home
Start Free TrialLog in
Avatar of businessesatoz
businessesatoz

asked on

how to add 1 to c++ Integer

Hello, i have a complete program that ask the user for the time. i'm trying to develope a function that will add 1 second to the users time.
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 for the function but it's not adding 1 to the seconds..
Here is my entire program so far:
#include    <iostream>
#include    <iomanip>
#include    <cstdlib>
#include  <string>
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;
    cout << "Please enter the time in HH:MM:SS format: ";
   if ((false == GetTime(userTime))  ||  (false == IsTimeValid(userTime)))
        {
        cout << "Invalid input..." << endl;
        exit(EXIT_FAILURE);
        }
   
   cout  <<userTime.seconds;
    //display the incremented time
    cout << "The incremented time is ";
    AddOneSecond(userTime);
    // DisplayTime(userTime);
    // return 0;

}  // end of main

void    AddOneSecond(Time  &timeParam)
{
	int addone=1;

	timeParam.seconds=addone+timeParam.seconds;
}

bool    IsTimeValid(const Time  &timeParam)
{
	if((timeParam.hours > 24) || timeParam.hours < 0)
	{
		return false;
	}
	else
	{
		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;
	    int minutes;
		int seconds;

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

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

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

Open in new window


here is the function i'm trying to do:
void    AddOneSecond(Time  &timeParam)
{
	int addone=1;

	timeParam.seconds=addone+timeParam.seconds;
}

Open in new window

But it's not adding 1 to the seconds..
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of businessesatoz
businessesatoz

ASKER

void    AddOneSecond(Time  &timeParam)
{
	timeParam.seconds=timeParam.seconds+1;
	
	if(timeParam.seconds == MAX_SECS)
	{
		timeParam.seconds=0;
		timeParam.mins=timeParam.mins+1;
	}

}

Open in new window

i have added this but i think i'm doing something wrong in my checking..
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It seems to me that you've asked why your code isn't incrementing the seconds member, which hasn't been addressed.

From what I see, your 'AddOneSecond' function should work.  However, how do you know it's not working (via debugger?)?  Where is 'DisplayTime' implementation?  In fact, it doesn't appear your code will even compile, since the main() function never returns a value.
I've requested that this question be closed as follows:

Accepted answer: 250 points for sarabande's comment http:/Q_27413488.html#37022587
Assisted answer: 0 points for businessesatoz's comment http:/Q_27413488.html#37022633
Assisted answer: 250 points for pkwan's comment http:/Q_27413488.html#37022642

for the following reason:

thank you for all your help.
accident.
thanks for your help.
pkwan, in questions with academical background we should not post full code samples. it is easier for both the expert and the questioner but is not the way experts in ee can go.

Sara
Sara,

noted. I will remember this next time. Thank you for reminding.
:)

businessesatoz, if you use a constant like MAX_SECS be aware that it must defined properly. in c++ i would suggest to using an enum constant like

enum {  MAX_SECS = 59 };

Open in new window


which could be placed either at top of the cpp file or in a header file if using one.

note, the 59 is necessary if you use > in the comparision as in the code of pkwan.

i am not sure whether one should use a constant in that case. 59 seconds always will be the maximum. so actually i would prefer using 59 rather than MAX_SECS cause it is easier to read and less error-prone as you see with one glance that it is not defined as 60.

be also aware that we have a last overflow case when the hours are greater 23. you need to reset to 0 in that case.

Sara