I can do this if I only know the length of this string |
I'm unclear what you don't know the length of. You're already using Students.length().
You can of course get the length of S1 by calling S1.length().
You can get the length of any substring by taking the difference between the locations of the semicolons.
However, there's a problem with your loop.
Line 14: You isolate the first subsctring to S1 and then erase those characters from Students.
That works fine on the first iteration. On subsequent iterations, i is now pointing to the wrong place (36), so on the second iteration you're pointing to the middle of the third student.
1 2 3
|
0....+....1....+....2....+....3....+....4....+....5....+
12002,Aly Hamed,(CSE121);12003,Omar Mohammed,(Math002)]
____________________________________^
|
Suggestion: Don't do the erase. Instead keep track of where the previous ; was and continue searching from there.
Line 16: S1 goes out of scope here.
Line 19: Your cout is in the wrong place. It should be after line 15. line 19 is displaying the S1 declared at 7, which is empty.