I have an practical due tomorrow and I was wondering if anyone could help me I cant seem to find the right way to start with this question, I know that I should use get line but I am not very good at it:
Write a programme that prompts the user to enter 3 first names, surnames and initials
(In that order on a single line per full name). Print the three names in the following
format: Surname, Initials, First name (Tip: Do not use >> with cin for string objects
containing spaces). The programme output should be as follows:
Please enter three full names:
Ralph Emerson W
Nelson Mandela R
Augustus de Morgan A
Formatted:
Emerson W Ralph
Mandela R Nelson
de Morgan A Augustus
that might be the case, but I cant seem to understand how the logic of the string works. I tried doing it a coupe of times but I always seem to mess something up.
Your program does have a problem displaying the second name and I hope you have the commons sense not to plagiarize this example as you might have a hard time coming with an explanation why you coded this way or be hard pressed to come up with something similar in the next exercise.
int main (void) {
char Entry [3][48], *Pos;
int I;
for ( I = 0; I < 3; I++) {
cout << "Name: [" << I + 1 << "] ";
cin.getline (Entry [I], 48); }
// Put a bit a space between entry and display
cout << "\n\n\n\n\n";
// Parse the entires according to exercise specifications
for ( I = 0; I < 3; I++ ) {
char *Given, *Initial;
Pos = Entry [I];
bool Status = false;
while ( *Pos ) {
if ( *Pos == ' ') {
*Pos++ = 0;
if ( Status == false )
{ Given = Pos; Status = true; }
else
{ Initial = Pos; break; }
}
else
Pos++;
}
// You may want to delete the part that puts a period after initial
// as it doesn't ask for that
cout << Given << " " << Initial << ". " << Entry [I] << endl;
}
return 0;
}
This just might give you a better idea how loops and conditionals are used to accomplish a job.