Im looking for a bit of code that could take an imputed name (first and last) and rearrange it (see example) before being saved in the array which can be recalled in the menu. I don't know where to put it, or even what to write for that matter. Helps appreciated. :)
Example:
User Input: Firstname Lastname
Output: Lastname, Firstname
like vlad said, i think you should've moved the other posts to this forum, as this is actually a beginner's issue, and you shouldn't post the same problem in multiple forums.
i didn't actually read your code, i think there's an easy way to do it:
1 2 3 4 5 6
char temp[20] = {'\0'};
string firstName, lastName;
cin.getline(temp , 19 , static_cast<char>(0x20)); //0x20 is ASCII for white space.
firstName = temp;
cin.getline(temp , 19 );
lastName = temp;
after this code you can manipulate the name as you see fit.
Thank you for your reply, and apologies for the double post, I am just desperate to find the answer for this. *Finals coming soon*. So I tried adding that code into my program to no avail, I tried formatting it into my void getNames function.
My teacher left a note saying:
"Upgrade your names program from lab 06 so that you can enter names like this “John Doe” at the keyboard but are converted to this “Doe, John” before being added to the storage list (array or vector). HINT: using str.find(substr, pos) function to find the ‘ ’ between the first name and last name entered; and then using str.substr(pos, num) function to extract the first name and last name; finally, concatenate the last name, ‘,’, and the first name to form the required string."
This is what is really giving me the issues, I don't know how to (or where to) incorporate str.find or str.substr into my code.
BTW. The code works, it takes names and everything, its just an issue of rearranging them.
you know what, me too have an asshole professor who needs an exact code.
i think he should accept any program that is:
1- readable.
2- working correctly on any input and outputs the correct demanded result.
the code i posted satisfies the two conditions, and if i were you, i'd take this code and ignore all about this ( str.find and str.substr ), this is an easy, short, and efficient code, without the use of these functions.
After all, he did say it was a hint, my problem now is in how to incorporate it into the getNames module. If you can help me with that, I am home free for this.
I am aware that after I can then cout lastname + ',' + firstname to get the result!
you know what, me too have an asshole professor who needs an exact code.
i think he should accept any program that is:
1- readable.
2- working correctly on any input and outputs the correct demanded result.
the code i posted satisfies the two conditions, and if i were you, i'd take this code and ignore all about this ( str.find and str.substr ), this is an easy, short, and efficient code, without the use of these functions.
The professor's job is to teach you how to program C++ as well as possible. Learning about STL objects like strings is an important part of modern C++ programming. If your professor is encouraging you to learn about them, then he's doing a good job.
i'm not gonna actually solve this for you, it's an assignment after all, i can give you some pointers:
after the code i posted gets executed, you will have the first name stored in firstName and the last name stored in lastName, if you want to output it as: firstname lastname
you can go like: cout<<firstName<<lastName;
otherwise you can write: cout<<lastName<<", "<<firstName;
furthermore, if you want to save the whole name in a string, you can use this:
well MikeyBoy, i'm sure you don't know anything about my specific professor.
first of all, he doesn't know shit about anything newer than standard C++ 1998, even this one, he doesn't know what the hell does STL stand for.
he demonstrated everything about stacks in 30 seconds and moved to another subject.
he gave one quick example about recursion and didn't even explain it enough, no one student understood it.
he encouraged us to use the C style headers (.h).
and this my friend is really so little of his madness, i don't even know how he got this job.
i really give all my respect to other professors, i just hate mine.