• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

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..
0
businessesatoz
Asked:
businessesatoz
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
sarabandeCommented:
you simplier can use the ++ operator like in i++; or ++i;

or use the += operator.

the more interesting part, though, is to check the overflow. when seconds reached 60 you need to reset to 0 and add 1 to the minutes (where again you might have an overflow).

Sara



0
 
businessesatozAuthor Commented:
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..
0
 
Peter KwanCommented:
You may check if the timeParam.seconds is larger than MAX_SECS, then reset it to 0 and add one to timeParam.mins. Similarly, you should check if the timeParam.mins is overflow after adding one to timeParam.mins.

The coding should be as following for your reference:

        if (timeParam.seconds > MAX_SECS) {
                timeParam.mins = addone + timeParam.mins;
                timeParam.seconds = 0;
                if (timeParam.mins > MAX_MINS) {
                        timeParam.hours = addone + timeParam.hours;
                        timeParam.mins = 0;
                }
        }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bill NolanCommented:
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.
0
 
businessesatozAuthor Commented:
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.
0
 
businessesatozAuthor Commented:
accident.
0
 
businessesatozAuthor Commented:
thanks for your help.
0
 
sarabandeCommented:
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
0
 
Peter KwanCommented:
Sara,

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

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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now