| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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?? |
|
#3
|
|||
|
|||
|
I need
int a = 19 int b = 10 int c =1984 |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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;
}
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > split string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|