Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 09-12-2004, 01:36 PM   #1 (permalink)
Crazy
 
Location: Arnold, MD
[c++] string into int?

Here is my delema, I scaned in a full line using getline, it would scan in something like 10:45 Pm, how can I take the 10 and put it into an int and then take the 45 and put it into another int? Thanks.
j0hnb is offline  
Old 09-12-2004, 01:43 PM   #2 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
There is a function called printf or sprintf that can help you convert strings to in. Google "String to int" or "Convert string to int".

Google is a programmers second best friend, second only to java...
__________________
"It better be funny"
kel is offline  
Old 09-12-2004, 03:45 PM   #3 (permalink)
I am Winter Born
 
Pragma's Avatar
 
Location: Alexandria, VA
The function atoi() turns a char* into an integer, with certain constraints.

There can be whitespace in front of the integer, but no alphabet characters. The number stops when a non-integer character is read.

Valid strings to pass to atoi():
" 103a39" -> 103
"9438745" -> 9438745
"-394" -> -394

Invalid strings:
"a5445a"
".25465"
Pragma is offline  
Old 09-12-2004, 05:56 PM   #4 (permalink)
Upright
 
Something else you might want to consider, parsing out "10:45 pm" as a string and using strptime() to convert it to a time structure.
bacon is offline  
Old 09-13-2004, 04:36 AM   #5 (permalink)
Crazy
 
Location: Arnold, MD
I didn't even know there was a time structure, i'll try it out, thanks
j0hnb is offline  
Old 09-13-2004, 05:01 AM   #6 (permalink)
Crazy
 
Use strtol().

(Error and sanity checks omitted)

Code:
char *ptr = "10:45pm";
char *endptr;
int hours, min;

hours = strtol(ptr, &endptr, 0);
/* endptr now set to ":45pm" */
if (endptr == NULL  || *endptr != ':')
       report_error("time format");
endptr++;
ptr = endptr;

min = strtol(ptr, &endptr, 0);

/* endptr now set to "pm" */
__________________
I want no escape.

Last edited by roboshark; 09-13-2004 at 05:07 AM..
roboshark is offline  
Old 09-13-2004, 10:24 AM   #7 (permalink)
Upright
 
Location: Boulder, Colorado
You could also use a stringstream:

Code:
#include <sstream>

// ...
// input = "10:45 pm"
std::stringstream buffer(input);
int iHours, iMinutes;
char c;
buffer >> iHours >> c >> iMinutes;
__________________
/K
ketamine is offline  
Old 09-14-2004, 04:48 PM   #8 (permalink)
a-j
Tilted
 
I think what you want is sscanf, which does the "opposite" what printf does
Something along the lines of (untested):

sscanf(string, "%i:%i", &hour, &min);
a-j is offline  
Old 09-16-2004, 01:49 PM   #9 (permalink)
MXL
Tilted
 
Location: Maryland
use sscanf, note: sscanf will return the number of tokens scanned.

CString sMyString = "10:45";
if (sscanf (sMyString, "%d:%d", &nHour, &nMinute) != 2)
{
// error
}
MXL is offline  
 

Tags
int, string


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
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 02:13 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62