This has been taking me forever to figure out and i have researched so many different examples, but some seem so convoluted and have extra steps in them for reasons A,B,C….. This is what I need-Let’s say you have a string and we will call it string A
String A = “I<Like<Butterscotch<candies<so<much”
So the delimiter of course is ‘<‘
How do I parse this string to take out the delimiters? I then have to make objects of these strings and add them to an array of pointers.
Please see an example below:) Please ignore all the directives at the top. I hate guessing what I need and don’t need so I started including every one I need in all files (except for including files within others). I have spent 6+ hours researching parsing and I am getting nowhere, so I am officially done for the night…..
I am also confused by pointers I guess because I thought the line below would be a pointer to take my data from the Student class in my student header file and have it create an Array of Pointers, which I will then fill with objects using the ‘add’ method below…….. but it tells me-Cannot initialize a member subobject of type ‘Student *’ with an rvalue of type ‘string *’ (aka ‘basic_string<char> *’)…. I feel like I am taking a step forward then hitting a wall and bouncing back 2 steps while trying to learn this material…..
Student* classRosterArray = new string[5];
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include <print>
#include <iomanip>
#include "student.hpp"
using namespace std;
class Roster
{
private:
// Array of pointers, size = 5-- I thought I was supposed to use a pointer to reference the Student class.... and call the new array which we are adding objects into classRosterArray?
Student* classRosterArray = new string[5];
//string* classRosterArray = new string[5]; --Is this what is needed??
public:
void parse(string row);
void add(string studentId,
string firstName,
string lastName,
string emailAddress,
int age,
int daysToComp3Course,
DegreeProgram degreeProgram);
};
user25485370 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
How do I parse this string to take out the delimiters?
To extract each word, use std::getline and std::istringstream:
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::string A = "I<Like<Butterscotch<candies<so<much";
std::istringstream strm(A);
std::string word;
while (std::getline(strm, word, '<'))
std::cout << word << "n";
}
Output:
I
Like
Butterscotch
candies
so
much
I’m sure there some fancy STL way to do this, but frankly, this is exactly strtok() from the C standard library and I’d just use that, except that in this century, I’d use strtok_r so as not to break multithreading. If you are on Windows, then you put up with what you’re handed.
https://man7.org/linux/man-pages/man3/strtok.3.html
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtok-strtok-l-wcstok-wcstok-l-mbstok-mbstok-l?view=msvc-170
You can also do similar things with strchr() to locate the first appearance of a char in a string. Better, there is also strsep(), which can manage multiple separators concurrently and was designed as a replacement for strtok to better handle its many shortcomings.
Things get a little more interesting if you are in the real world trying to handle unicode or right to left text systems, but I think I would look for more OS-specific solutions for that such as [NSString enumerateSubstringsInRange:options:usingBlock:]. This particular problem looks like someone’s homework.