C++ Assignment storing strings

Mar 2, 2010 at 12:49am
Hi, sorry to bother everyone but I am new at C++ and Have no idea how to do this.

2. Write a program that stores a string containing a full name (First Middle Last) with a single space between any two successive names then it outputs each part of the name in a separate line. For testing purpose, your program MUST use the following name “Henry Louis Aaron” where the output in this case will as given below. Make sure that your program should function properly in case you use any other full name (it should be generic).

First Name: Henry
Middle Name: Louis
Last Name: Aaron
Hint: You need to use various string manipulation functions.


any help is appreciated!
Mar 2, 2010 at 12:56am
A good start is declaring three string variables and you could call them first, middle, and last.

1
2
3
#include <string>

string first, middle, last;

Mar 2, 2010 at 1:08am
Can anyone give me more help. I'm still stumped.
Mar 2, 2010 at 1:51am
bump
Mar 2, 2010 at 1:55am
1
2
3
4
 
string first, middle, last;
cout << "Enter you full name: ";
cin >> first >> middle >> last;


or

1
2
3
string name;
cout << "Enter you full name: ";
getline (cin, name);
Last edited on Mar 2, 2010 at 1:56am
Mar 2, 2010 at 1:59am
how do i substitute his name in for first, middle and last for the strings to get the output?? im still a little confused..
Mar 2, 2010 at 2:04am
Is that the whole question? it says you need to use various string manipulation functions yet I just don't see the need for any...

ok they say we must use the name given so perhaps they mean concatenation:
1
2
3
4
5
6
7
8
9
string first="Henry";
string middle="Louis";
string last="Aaron";

string name;

name = first + " " + middle + " " + last;

cout << "Name is: " << name << endl;
Last edited on Mar 2, 2010 at 2:04am
Mar 2, 2010 at 2:08am
lol
Last edited on Mar 2, 2010 at 2:24am
Mar 2, 2010 at 2:23am
plzzz help me im so confused.... and theres alot of errors in my code can anyone help?? this is what i got so far.

#include <string>

string first, middle, last;
cout << "Enter you full name: ";
cin >> first >> middle >> last;

string first="Henry";
string middle="Louis";
string last="Aaron";

string name;

name = first + " " + middle + " " + last;

cout << "Name is: " << name << endl;
Mar 2, 2010 at 2:32am
You are trying to declare the same string variables...get rid of "string" before you assign the names to the variables.
Mar 2, 2010 at 2:35am
which string?? can you be more specific please like im really a noob at this so idk what your talking about.
Mar 2, 2010 at 2:42am
I wrote a very simple code but it gets the job done
even if the person has a longer name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main()
{
    int i = 0, m, l;    
    char strNames[50];
    gets(strNames);
    
    
    printf("First Name: ");
    for( ; strNames[i] != 32; i++ )
         printf("%c", strNames[i]);
    m = i;
    for( ; strNames[i] != '\0'; i++ );
    for( ; strNames[i] != 32; i-- );
    l = i;
    printf("\nMiddle Name:");
    for( ; m <= l; m++ )
         printf("%c", strNames[m]);
    printf("\nLast Name:");     
    for( ; strNames[l] != '\0'; l++ )
         printf("%c", strNames[l]);
getchar();
return 0;
}
Mar 2, 2010 at 2:42am
try this

edit:Cshark beat me too it... even though his is in C and I can't understand it XD
Last edited on Mar 2, 2010 at 2:47am
Mar 2, 2010 at 2:51am
I need this in C++ language!!!
Last edited on Mar 2, 2010 at 2:51am
Mar 2, 2010 at 2:56am
give me 10 minutes and ill have it done

edit:sorry i don't have time. If i can ill do it in the morning
Last edited on Mar 2, 2010 at 3:04am
Mar 2, 2010 at 3:12am
can anyone else translate this C into C++?? please!
Mar 2, 2010 at 4:46am
Topic archived. No new replies allowed.