I was trying to solve a problem. The Problem is : There is given a date string like 21/9/2013. I have to convert this date into int. I have used stoi
but it is just showed first two int 21.
Asked
Active
Viewed 136 times
0

Md Asaduzzaman
- 33
- 5
-
1What is the int supposed to represent ? Eg what int value would you want to produce given your example date? – Jeremy Friesner Apr 23 '21 at 04:07
-
The output should be 21 9 2013 – Md Asaduzzaman Apr 23 '21 at 04:08
-
2That’s three ints – Jeremy Friesner Apr 23 '21 at 04:09
-
yes that's three int only – Md Asaduzzaman Apr 23 '21 at 04:10
-
What exactly are you supposed to return? As Jeremy said, 21 9 2013 is three ints, not one int. C++ doesn't let you return 3 ints. Do you want to return a string of these three ints? An array of 3 ints? Do you want to print 21 9 2013 to the console? – jdabtieu Apr 23 '21 at 04:12
-
@jdabtieu I want to print 21 9 2013 to the console – Md Asaduzzaman Apr 23 '21 at 04:13
2 Answers
1
What you want to do here is tokenize the string into three different substrings ("21", "9", and "2013"); then you can call stoi()
on each substring and print out the integer stoi()
returned for each one.
There are various ways to tokenize a string in C++; rather than choose one to repeat here, I'll just link to the StackOverflow question and answers on that topic.

Jeremy Friesner
- 70,199
- 15
- 131
- 234
1
I want to print 21 9 2013 to the console
So you don't really need three integers. You need three strings.
With these includes:
#include <string>
#include <sstream>
using namespace std;
You can parse a string like this:
string date = "21/9/2013";
into its components, like this:
stringstream stream(date);
string s;
while (std::getline(stream, s, '/') {
cout << s << " ";
}
cout << endl;
The above should print out: 21 9 2013
If you really want integers, the above shouldn't be too hard to modify. You can use stoi on each iteration of the while loop above.

selbie
- 100,020
- 15
- 103
- 173