Okay, So I've looked around on StackOverflow and I've stumbled across a way of splitting C via delimiters.
So far, I've looked at these, and I still don't understand it.
- Parse (split) a string in C using string delimiter (standard C )
- https://www.oreilly.com/library/view/c-cookbook/0596007612/ch04s07.html
- C spliting string by delimiters and keeping the delimiters in result
- split a C string into two integers, which are delimited by ":"
From my understanding, I need to use a delimiter, using a variable that houses the delimiter, and then use the substr() method/function, but I don't understand the whole thing.
For instance, I saw this one example where it was referencing pos and npos, I don't understand that. And my other issue is, I wouldn't know how to do it with a string with multiple copies of the same delimiter.
My goal is to take a date like this: "29/01/2022 • 05:25:01" to split it into a struct for date and time, eg:
struct Date
{
int day; //Integer for days
int month; //Integer for months
int year; //Integer for years
};
struct Time
{
int hour; //Integer for hour of drop
int minute; //Integer for minute of drop
int second; //Integer for second of drop
int milisecond; //Integer for milisecond of drop
};
I've also looked at https://www.cplusplus.com/reference/, however I want to split it up so that they are stored in their own variables, eg:
string example
{
struct Date D;
struct Time T;
D.Day = 29;
D.Month = 01;
D.Year = 2022;
T.Hour = 5;
T.Minute = 25;
T.Second = 01;
}
Would someone be able to explain this to me in a simpler way, or show me a source that explains it easier? The main problem I have is not understanding certain words.
Any help is appreciated, I really am trying to learn, but I don't quite understand these subjects yet.
CodePudding user response:
Let's go step by step, starting with the date:
29/01/2022 -- Day, Month, Year.
Given the following:
unsigned int day = 0u;
std::cin >> day;
The input of an integer skips whitespace until the first number character (for the first number character, also includes ' ' and '-'). The extraction operator keeps reading characters, building a number, until a non-numeric character is reached:
2 --> day. 9 --> day.
The next character is '/', which is not a numeric character so the extraction operator returns the number 29.
The character '/' in this context is known as a delimiter, because it separates the day field from the month field.
Since it's a character, it has to be read using a character variable:
char delimiter = '\0';
std::cin >> delimiter;
Now, the delimiter is no longer in the buffer. You can check the content of the delimiter variable or move on.
Reading the month is similar:
unsigned int month = 0U;
std::cin >> month;
Edit 1: delimiter and substrings
You could extract the month as a string using a delimiter:
std::string month_as_text;
std::getline(std::cin, month_as_text, '/');
The getline function above reads characters from std::cin, placing into the string month_as_text, until it finds the delimiter character '/'. You can then convert month_as_text into an integer variable.
