C++ Homework question

Hi
I have a homework problem that just got me stumped.

How do I write a C++ program that takes a string containg a full name, and outputs each part of the name seperately?

const string fullname= "Horatio Thadeous Hornblow"

using only simple codes like length, size, adding, and subtracting.
I can't use loop or i will get a deduction

What i use is ;

const string fullname= "Horatio Thadeous Hornblower";

string firstname;

string middlename;

string lastname;

int spacePos= fullname.find(' ');

firstname = fullname.substr(0, spacePos);

middlename = fullname.substr(spacePos + 1, .........

and that is where i got stuck, getting the middle name by itself
You would have to search for the next ' ' character in your string again, basically updating your pointer to stop when it reaches it, as you did for the first name, printing the middle characters. Then you will be able to print the remainder as the last name.

Hope this helps some
Dear All

I need a program in C++ that works.

It should be 1000 line and the subject can be anything,

Please get back to me on [email protected]
well, you see I can not use loop to find the next space. is there a way by findinf the length of the first name and space and subtract the full name to another variable and then find the 2nd space.
Last edited on
Hi rite2bobby,

Just so you know, it's not considered polite to post a question in more than one forum. Naughty, naughty...

You need to choose which is the best forum for your question and post it once.

In this case the beginners forum was the right place (and you'll notice I answered it there) but cross-posting to more than one forum will simply annoy people and you'll only get rude responses, if any at all.

Regards,
muzhogg
how about

int spacepos = fullname.find(' ');

string fn = fullname.subtr(spacepos, 100);

int spacepos2 = fn.find(' ');

firstname= fullname.subtr(0, spacepos);

middlename= fn.substr(0 , spacepos2);

lastname= fn.subst(spacepos2, 100);

and im sorry for posting again
Yup, something like that was what I was thinking, although you don't have to create a second variable called spacepos2, you can merely overwrite the spacepos variable, unless it is going to be recalled again.
Topic archived. No new replies allowed.