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: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

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

 
0
  #1
Nov 21st, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,989
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

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

 
0
  #2
Nov 21st, 2007
  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!



Regards Niek
Last edited by niek_e; Nov 21st, 2007 at 8:12 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

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

 
0
  #3
Nov 21st, 2007
I'm stupid, and can see how to do it,

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

sorry everyone ! :-)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 26
Reputation: alcoheca is an unknown quantity at this point 
Solved Threads: 0
alcoheca alcoheca is offline Offline
Light Poster

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

 
0
  #4
Nov 21st, 2007
Originally Posted by niek_e View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,989
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

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

 
0
  #5
Nov 21st, 2007
Here's a link to the istringstream class

For your problem I'd probably use get() or ignore().

regards Niek
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2840 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC