| | |
atoi(string.substr(x,x)) Possible?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 26
Reputation:
Solved Threads: 0
Hi I wish to break up a date entered as a string into ints for day, month, year.
String to be entered follows this convention DDMMYYYY - this will be checked so we can presume it's suitable.
I have it working using tempary variables, but is there a way to do it in one call?
String to be entered follows this convention DDMMYYYY - this will be checked so we can presume it's suitable.
I have it working using tempary variables, but is there a way to do it in one call?
c Syntax (Toggle Plain Text)
Person getPerson() { Person person; string dob, temp; int date; cout << "\nEnter name: "; getline(cin, person.name); cout << "\nEnter title: "; getline(cin, person.title); do { cout << "\nEnter DOB (DDMMYYYY): "; cin >> dob; if (atoi(dob.c_str())) break; } while (!atoi(dob.c_str())); temp = dob.substr(0,2); cout << temp; // Sanity check person.dob.d = atoi(temp.c_str()); //person.dob.m = atoi(dob.substr(2,3)); Doesn't work : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>::_Myt' to 'const char *' //person.dob.y = atoi(dob.substr(4,7)); return person; }
That's one way to do it. I would do it like this:
And voila: string dob is now saved as an int in intDob!
Regards Niek
c Syntax (Toggle Plain Text)
istringstream convert(dob); int intDob =0; convert >> intDob
Regards Niek
Last edited by niek_e; Nov 21st, 2007 at 8:12 am.
•
•
Join Date: Nov 2007
Posts: 26
Reputation:
Solved Threads: 0
•
•
•
•
That's one way to do it. I would do it like this:
And voila: string dob is now saved as an int in intDob!c Syntax (Toggle Plain Text)
istringstream convert(dob); int intDob =0; convert >> intDob
![]() |
Similar Threads
- String to Int (C++)
- String class (C++)
- Splitting a string? (C)
- converting the date to an int (C++)
- VB's Left, Right, Mid Functions in C++? (C++)
- Exception Handling (C++)
- Searching in documents... (C++)
Other Threads in the C++ Forum
- Previous Thread: "breaking up" your application
- Next Thread: check if a dll file is currently in use
Views: 2840 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






