C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 28th, 2004, 01:15 PM
erviins erviins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 3 erviins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
split string

Hi,

i have char date[30];
date has value 19.10.1984.

How can I split this char date - 19.10.1984 in int array?

Thanks,

Ervins

Reply With Quote
  #2  
Old December 29th, 2004, 03:18 AM
popeye137 popeye137 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Queen City of the South--> Cebu Philippines
Posts: 11 popeye137 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 1 m 22 sec
Reputation Power: 0
string split

hi Ervins,

can you please tell us what do you want the output of 19.10.1984 would be?

do you want it to be 19101984? or what??

Reply With Quote
  #3  
Old December 29th, 2004, 07:02 AM
erviins erviins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 3 erviins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I need
int a = 19
int b = 10
int c =1984

Reply With Quote
  #4  
Old January 5th, 2005, 03:49 PM
XZminX XZminX is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Location: Varaždin, Croatia
Posts: 5 XZminX User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to XZminX Send a message via AIM to XZminX Send a message via MSN to XZminX
Hi there!
You dont need a char for that (not that I say it cant be done that way, but its a bit complicated).
here's the code:

int date,a,b,c;
scanf("%d.%d.%d",&a,&b,&c);

well thats it. the dots inside the quotes means that the program will take these dots as a beginning of a new integer.
That was in C, if u're using C++ just import biblioteque called cstdio (#include<cstdio>)

If you must do it with chars, just say and I'll post the solution here.

Reply With Quote
  #5  
Old January 11th, 2005, 12:20 PM
maxmax maxmax is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 14 maxmax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 17 sec
Reputation Power: 0
need some help

hey
i have a problem which i think is pretty close to waht you guys are talking about.

does anyone know if there is an equivalent of cin.getline for integer arrays?
i have an int array that i want to allow the user to fill in, but i need it to be parsed into only 4 digits,
so if the user types in 4657 array[1] would be 4, array[2] would be 6, and so on and so on.

if nobody knows how to do that, then i could really use some advice on how to convert char arrays to int
arrays becuase thats the problem that results in my program when i use cin.getline and a char array containing
integer values. ive tried just saying int array[1] == char array[1] but that doesnt work.

anyways if anybody can help it would be much appreciated.
thanks

Reply With Quote
  #6  
Old January 24th, 2005, 10:29 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
this should work, and it compiles with dev-c++ beta 5, but then it causes a fatal error
o well, the function u want is strtok(), go to http://www.cplusplus.com/ref/cstring/strtok.html to learn more about it

Code:
  #include <iostream> //for cout and cin
  #include <string> //for strtok()
  #include <cstdlib> //for atoi()
  using namespace std;
  
  int main() {
  	char* date = ""; //declare variable which will hold the un-splitted date as a string
  	char* date_ar[3] = {"", "", ""}; //declare the array which will hold the parts of the date as strings
 	cout << "Please enter a date in the format DD.MM.YYYY: "; //prompt the user to enter the date in the correct format
 	cin >> date; //grab the date input from the user NOTE: we are trusting the user to use the correct format, if they don't weird things will probably happen after this point
 	date_ar[0] = strtok(date, "."); //refer to http://www.cplusplus.com/ref/ for information on what this does, i don't completely understand it but their example code does what you want
  	date_ar[1] = strtok(NULL, ".");
  	date_ar[2] = strtok(NULL, ".");
  	int a = atoi(date_ar[0]);
  	int b = atoi(date_ar[1]);
 	int c = atoi(date_ar[2]); //declare variables to hold the parts of the date as ints and fill them with the results of atoi(), which converts a string to an integer
  	cout << "you entered " << a << "." << b << "." << c << endl;
  	cin.get();
  	return 0;
  }
  

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > split string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT