944,156 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5113
  • C++ RSS
Nov 21st, 2007
0

atoi(string.substr(x,x)) Possible?

Expand Post »
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?

  1. Person getPerson() {
  2. Person person;
  3. string dob, temp;
  4. int date;
  5. cout << "\nEnter name: ";
  6. getline(cin, person.name);
  7. cout << "\nEnter title: ";
  8. getline(cin, person.title);
  9. do {
  10. cout << "\nEnter DOB (DDMMYYYY): ";
  11. cin >> dob;
  12. if (atoi(dob.c_str())) break;
  13. } while (!atoi(dob.c_str()));
  14.  
  15. temp = dob.substr(0,2);
  16. cout << temp; // Sanity check
  17. person.dob.d = atoi(temp.c_str());
  18. //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 *'
  19. //person.dob.y = atoi(dob.substr(4,7));
  20. return person;
  21. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

Re: atoi(string.substr(x,x)) Possible?

Quote ...
  1. atoi(dob.c_str())
That's one way to do it. I would do it like this:

  1. istringstream convert(dob);
  2. int intDob =0;
  3. convert >> intDob
And voila: string dob is now saved as an int in intDob!
Last edited by Nick Evan; May 11th, 2010 at 8:42 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Nov 21st, 2007
0

Re: atoi(string.substr(x,x)) Possible?

I'm stupid, and can see how to do it,

atoi(dob.substr(2,3).c_str());

sorry everyone ! :-)
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

Re: atoi(string.substr(x,x)) Possible?

Click to Expand / Collapse  Quote originally posted by niek_e ...
That's one way to do it. I would do it like this:

  1. istringstream convert(dob);
  2. int intDob =0;
  3. convert >> intDob
And voila: string dob is now saved as an int in intDob!
I know I marked this as solved, but if I used the istringstream method, how would I then split the integer into individual ints for d, m & y?
Reputation Points: 10
Solved Threads: 0
Light Poster
alcoheca is offline Offline
26 posts
since Nov 2007
Nov 21st, 2007
0

Re: atoi(string.substr(x,x)) Possible?

Here's a link to the istringstream class

For your problem I'd probably use get() or ignore().
Last edited by Nick Evan; May 11th, 2010 at 8:42 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: "breaking up" your application
Next Thread in C++ Forum Timeline: check if a dll file is currently in use





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC