For something this simple, you could try using rfind(' ') which does almost exactly the same thing as find(' ') _except_ it starts at the end of the string. So;
1 2 3 4 5 6 7
int spacePos1 = fullname.find(' '); // gets position of FIRST space
int spacePos2 = fullname.rfind(' '); // gets position of LAST space
int middlelength = spacePos2 - spacePos1; // calculates length of name
// NOW output "middlelength" chars starting at position "spacePos1+1"
// "spacePos +1" and "middlelength-1" because we DON'T want to include
// spaces: i.e. we want "middlename" NOT " middlename "
middlename = fullname.substr(spacePos1 + 1, middlelength - 1);
ok that is great with the int spacPos2 = fullname.rfind(' ');......// but i cant use .rfind to find it again or I will get a deductions. how about a way; of fullname length - (firstname + spacepos1) as a new variable and then search for the spacepos2 there?
Sorry, I don't understand the problem you're now trying to solve. That is, I'm not sure what you mean when you say you can't use rfind() "again". I'm suggesting that you use find() and rfind() only once each;
using find(' ') will locate the position of the first space.
using rfind (' ') will locate the position of the second space.
and with that information surely you can use substr() to locate all three names in the string?
Or do you mean that you are not allowed to use rfind() AT ALL in this program?
If you want to avoid using rfind() at all, I think your suggestion above will work quite well -- although you need a couple of slight adjustments;
1 2 3 4 5 6 7 8 9 10 11 12 13
int spacepos = fullname.find(' '); // locate first space
firstname = fullname.substr(0, spacepos); // get from start to space
fn = fullname.substr(spacepos+1); // get a shorter string (omit " ")
spacepos = fn.find(' '); // find first space in shorter string
middlename = fn.substr(0, spacepos); // get from start to space
lastname = fn.substr(spacepos+1); // get from space to end (omit " ")
Note that I understand what you're trying to do with
fn = filename.substr(spacepos, 100);
and
lastname= fn.subst(spacepos2, 100);
but it's bad practice to try to read 100 characters without being sure where the end of the string is.
You should simply use;
fn = filename.substr(spacepos+1);
and
lastname = fn.substr(spacepos+1);
which will read from one char after the space to the end of the string anyway.
well, my class is a beginner class and the teacher said we cannot use code we have not covered and only use the ones we were taught. And this is the first time i saw .rfind function, but i will ask my prof if it is allowed.
I wouldn't ask -- he'll probably figure out that you've been looking for help on forums :) and might think you simply stole somebody else's answer -- which you didn't, you worked it out on your own with a little help.
Most teachers support the idea of using the internet as a valid resource--as in the "real world", you can use it to look up functions, syntax, get help, etc. As long as you are able to explain the code, I would think that the instructor would not have a problem with it (although I guess that every instructor is different).