Raised This Month: $32 Target: $400
 8% 

Solved Copy first 2 characters of a string into another string


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-16-2018 , 12:05   Copy first 2 characters of a string into another string
Reply With Quote #1

I'm making a stock to add up minutes to a given time, but I need a way to copy first 2 characters of a string into another one.

I made this code, but the part where we transfer hours and minutes into individual strings doesn't work.
PHP Code:
AddMinutesToTime( const szTime[ ], const iMinutesToAddszReturnTime[ ], const iLen )
{
    new 
szHour], szMinutes];

    
// szTime[ 0 ] = 0
    // szTime[ 1 ] = 1
    // szTime[ 2 ] = :
    // szTime[ 3 ] = 4
    // szTime[ 4 ] = 1
    
    
if( szTime] == )
    {
        
szHour] = szTime];
    }
    
    else if( 
szTime] != )
    {
        
szHour] = szTime];
        
szHour] = szTime];
    }
    
    if( 
szTime] == )
    {
        
szMinutes] = szTime];
    }
    
    else if( 
szTime] != )
    {
        
szMinutes] = szTime];
        
szMinutes] = szTime];
    }
    
    new 
iTimeMinutes str_to_numszMinutes );
    new 
iTimeHour str_to_numszHour );

    
log_to_file"Hour.txt""#1 Minutes: %d | Hours: %d"iTimeMinutesiTimeHour );
    
    new 
iFinalHouriFinalMinutes;
    
    new 
iToAdd iTimeMinutes iMinutesToAdd// 5:32 + 200
    
    
if( iToAdd 59 // 232 > 59
    
{
        new 
iDivide iToAdd 60// 232 / 60 = 3
        
        
iFinalHour iTimeHour iDivide// 5 -> 8
        
iFinalMinutes = ( iToAdd - ( 60 iDivide ) ); // 232 - (60*3) == 232-180 = 52
        
        
log_to_file"Hour.txt""#2 Minutes: %d | Hours: %d"iFinalMinutesiFinalHour );
    }
    
    
formatexszReturnTimeiLen"%d:%d"iFinalHouriFinalMinutes );

Code:
L 08/16/2018 - 18:57:10: #1 Minutes: 4 | Hours: 1
L 08/16/2018 - 18:57:10: 0:0
__________________

Last edited by edon1337; 08-16-2018 at 12:28.
edon1337 is offline
Ghosted
Veteran Member
Join Date: Apr 2015
Location: Georgia
Old 08-16-2018 , 12:08   Re: Copy first 2 characters of a string into another string
Reply With Quote #2

You want Time String (03:43) to Hour & Minute Numbers?
__________________

[MOD] CS Weapon Mod V1.7.1
[MM] MetaMod-C V1.0
[MOD] CS NPC Mod (5%)


Probably Left AM

Last edited by Ghosted; 08-16-2018 at 12:13.
Ghosted is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-16-2018 , 12:15   Re: Copy first 2 characters of a string into another string
Reply With Quote #3

Quote:
Originally Posted by Ghosted View Post
You want Time String (03:43) to Hour & Minute Numbers?
03 to be in szHour string and 43 in szMinutes string, note that we have to get rid of the first zeros.

Ex: 03:09 - > 3:9
__________________
edon1337 is offline
Ghosted
Veteran Member
Join Date: Apr 2015
Location: Georgia
Old 08-16-2018 , 12:17   Re: Copy first 2 characters of a string into another string
Reply With Quote #4

Quote:
Originally Posted by edon1337 View Post
03 to be in szHour string and 43 in szMinutes string, note that we have to get rid of the first zeros.

Ex: 03:09 - > 3:9
new szHours[3], szMinutes[3] // 3rd = string terminator
num_to_str(str_to_num(szTime), szHours, charsmax(szHours));
num_to_str(str_to_num(szTime[2]), szMinutes, charsmax(szMinutes));
__________________

[MOD] CS Weapon Mod V1.7.1
[MM] MetaMod-C V1.0
[MOD] CS NPC Mod (5%)


Probably Left AM
Ghosted is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-16-2018 , 12:20   Re: Copy first 2 characters of a string into another string
Reply With Quote #5

Quote:
Originally Posted by Ghosted View Post
new szHours[3], szMinutes[3] // 3rd = string terminator
num_to_str(str_to_num(szTime), szHours, charsmax(szHours));
num_to_str(str_to_num(szTime[2]), szMinutes, charsmax(szMinutes));
Well that logically wasn't supposed to work but I tested to prove myself right.
PHP Code:
#1 Minutes: 0 | Hours: 1 
While it should be
PHP Code:
#1 Minutes: 1 | Hours: 41 
I guess you didn't understand me:

I have szTime[ 5 ]

where szTime[ 0 ] and szTime[ 1 ] should be transferred to szHours while szTime[ 3 ] and szTime[ 4 ] should be transferred to szMinutes
__________________

Last edited by edon1337; 08-16-2018 at 12:23.
edon1337 is offline
Ghosted
Veteran Member
Join Date: Apr 2015
Location: Georgia
Old 08-16-2018 , 12:24   Re: Copy first 2 characters of a string into another string
Reply With Quote #6

Quote:
Originally Posted by edon1337 View Post
Well that logically wasn't supposed to work but I tested to prove myself right.
PHP Code:
#1 Minutes: 0 | Hours: 1 
While it should be
PHP Code:
#1 Minutes: 1 | Hours: 41 
I guess you didn't understand me:

I have szTime[ 5 ]

where szTime[ 0 ] and szTime[ 1 ] should be transferred to szHours while szTime[ 3 ] and szTime[ 4 ] should be transferred to szMinutes
szTime[2] => szTime[3]
__________________

[MOD] CS Weapon Mod V1.7.1
[MM] MetaMod-C V1.0
[MOD] CS NPC Mod (5%)


Probably Left AM
Ghosted is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-16-2018 , 12:28   Re: Copy first 2 characters of a string into another string
Reply With Quote #7

Quote:
Originally Posted by Ghosted View Post
szTime[2] => szTime[3]
Thank you, worked.
__________________
edon1337 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 08-16-2018 , 21:57   Re: Copy first 2 characters of a string into another string
Reply With Quote #8

Note that if you want to store a 2 digit number as a string, you need an array size of 3 (i.e. szHour and szMinutes). I'm not sure how str_to_num() handles invalid strings but maybe it's ok since you say your code is working.

Overall, it looks like you are doing way more work than necessary for both parsing of the incoming string (instead, use strtok()) and for calculating the resulting time (instead, use modulus). See the simplified version below:

Code:
public AddMinutesToTime( const szTime[ ], const iMinutesToAdd, szReturnTime[ ], const iLen )
{
	// szTime[0] = '0'
	// szTime[1] = '1'
	// szTime[2] = ':'
	// szTime[3] = '4'
	// szTime[4] = '1'
	// szTime[5] = EOS

	new szHours[3], szMinutes[3], iHours, iMinutes;
	strtok(szTime, szHours, charsmax(szHours), szMinutes, charsmax(szMinutes), ':')  // Parse the string for 'minutes' and 'hours'
	
	iHours = str_to_num( szHours );
	iMinutes = str_to_num( szMinutes );
	
	iMinutes += iMinutesToAdd  // Add the requested minutes
	
	iHours += iMinutes / 60  // Calculate and add the number of hours from iMinutes
	iMinutes = iMinutes % 60  // Remove the hours that was added to iHours from iMinutes
	
	formatex(szReturnTime, iLen, "%d:%d", iHours, iMinutes);
}
__________________
fysiks is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 08-17-2018 , 03:46   Re: Copy first 2 characters of a string into another string
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
Note that if you want to store a 2 digit number as a string, you need an array size of 3 (i.e. szHour and szMinutes). I'm not sure how str_to_num() handles invalid strings but maybe it's ok since you say your code is working.

Overall, it looks like you are doing way more work than necessary for both parsing of the incoming string (instead, use strtok()) and for calculating the resulting time (instead, use modulus). See the simplified version below:

Code:
public AddMinutesToTime( const szTime[ ], const iMinutesToAdd, szReturnTime[ ], const iLen )
{
	// szTime[0] = '0'
	// szTime[1] = '1'
	// szTime[2] = ':'
	// szTime[3] = '4'
	// szTime[4] = '1'
	// szTime[5] = EOS

	new szHours[3], szMinutes[3], iHours, iMinutes;
	strtok(szTime, szHours, charsmax(szHours), szMinutes, charsmax(szMinutes), ':')  // Parse the string for 'minutes' and 'hours'
	
	iHours = str_to_num( szHours );
	iMinutes = str_to_num( szMinutes );
	
	iMinutes += iMinutesToAdd  // Add the requested minutes
	
	iHours += iMinutes / 60  // Calculate and add the number of hours from iMinutes
	iMinutes = iMinutes % 60  // Remove the hours that was added to iHours from iMinutes
	
	formatex(szReturnTime, iLen, "%d:%d", iHours, iMinutes);
}
Cool. Unfortunately I don't get how modulus operator works so instead I have to make more calculations.
__________________
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 08-17-2018 , 05:12   Re: Copy first 2 characters of a string into another string
Reply With Quote #10

Instead of saying "I don't get how modulus operator works" you should learn it, it's a basic concept that's taught in elementary school when you are like 11 or so (remember division remainders?). Same goes for your other topic where you reject the answer because you don't understand Unix timestamps. Learn the right way to do things, operations on time/time spans should only be done in Unix timestamps.
__________________
klippy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:34.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode