help with a program

we need a program that takes three names ( first middle last ) and print it each one at a different line...
URGENTT
what have you got so far? have you read the names in via user input:
http://www.cplusplus.com/doc/tutorial/basic_io/
?

or using command line arguments:
http://www.cprogramming.com/tutorial/lesson14.html
?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main ()
{
string a;
char j[100];
int i, c, b;
cout <<"enter your name ";
getline(cin,a);
cout << " welcome " << a << endl;
c=a.size;
for (b=0; b<=c; b++)
j[b]=a[b];
i[b]='\0';
system ("pause");
return 0; */
}
I suggest you read through some of the tutorials on this site. See the link on the left.
string str1;
string str2;
string str3;

cout << "Enter your First, Middle and Last name." << endl;

cin >> str1;
cin >> str2;
cin >> str3;

cout << endl;

cout << str1 << endl;
cout << endl;

cout << str2 << endl;
cout << endl;

cout << str3 << endl;
cout << endl;

Try to use this and if this works, try to understand or ask questions for you to learn and not just simply copy this.

This will handle multiple word first names by the way but can not handle middle or last name with multiple words.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    string name;
    vector<string> tokens;

    cout << "Enter a full name (Format: First Middle Last): ";
    getline(cin, name);

    // break line into words and save to a vector array
    char *token = strtok((char *)name.c_str(), " ");
    while (token) {
        // save token to array
        string s(token);
        tokens.push_back(s);

        // get next token
        token = strtok(NULL, " ");
    }

    // Get first name and print
    cout << "First name: ";
    for (int i = 0; i < tokens.size() - 2; i++) {
       cout << tokens[i] << " ";
    }
    cout << endl;

    //Get middle name and print
    cout << "Middle name: " << tokens[tokens.size() - 2] << endl;

    //Get last name and print
    token = strtok(NULL, " ");
    cout << "Last name: " << tokens[tokens.size() - 1] << endl;

    return 0;
}
Topic archived. No new replies allowed.