I need to input 3 full names separated by commas

Full Name 1: John, Smith, Flynn
Full Name 2: Walter, Kennedy, Roberts
Full Name 3: Sam, Bass, Clinton


Then output it like this

First Name 1: John
First Name 2: Walter
First Name 3: Sam


Middle Name 1: Smith
Middle Name 2: Kennedy
Middle Name 3: Bass


Last Name 1: Flynn
Last Name 2: Roberts
Last Name 3: Clinton


How do i do these? so far this is my code

include #<iostream>
include #<cstdio>
include #<cstring>
using namespace std;

int main () { char first[3][100]; char middle[3][100]; char last[3][100]; char full[3][100]; int i;

cout << "Enter 3 Full Names <first, middle and last names separated by comma>:" << endl; for (i=0; i<3; i++) {  cout << "Full Name " << i+1 << ":" ; gets (full[i]); }

cout << "The first names are: " << endl; for (i=0; i<3; i++) {     strcpy (first[i], full[i]);   if (strcmp (first[i], ", ")) {     cout << "First Name "<< i+1 << ":" ;  strcpy ( first[i], full[i] );  cout << (first[i]);  cout << endl; } } cout << "The middle names are: " << endl; for (i=0; i<3; i++) {

cout << "Middle Name "<< i+1 << ":" ; cout << (middle[i]); cout << endl; }

cout << "The last names are: " << endl; for (i=0; i<3; i++) {

cout << "Last Name "<< i+1 << ":" ; cout << (last[i]); cout << endl; }

system("pause"); return 0; }

I'm assuming you are required to use the c-strings?

Check out strtok http://www.cplusplus.com/reference/clibrary/cstring/strtok/ (using the comma as a delimiter)

Also, never use gets(), you can overrun the buffer by a mile and corrupt the neighboring memory. Use fgets instead so that there is a fixed buffer size.

Normally, you would do this with a string stream (or the input stream if you like). For instance, getline will take a delimiter:

std::cout << "First names: ";
    std::string token;
    getline ( std::cin, token, ',' );
    std::cout << "Read token: " << token << std::endl;
    getline ( std::cin, token, ',' );
    std::cout << "Read token: " << token << std::endl;
    getline ( std::cin, token );
    std::cout << "Read token: " << token << std::endl;

Which produces

First names: Frank,Sylvia,Thomas
Read token: Frank
Read token: Sylvia
Read token: Thomas

This is not very robust (spaces will not be handled correctly) but it gives you an idea of where to start.

Here you go. Let me know if you find any problems - I wrote this while half asleep.

#include <iostream>
#include <vector>
using namespace std;

bool isDelim(const char& query, const string& delimList){
    for(int i = 0; i < delimList.length(); i++)
        if(query == delimList[i])
            return true;
    return false;
}

vector<string> split(const string& line, const string& delimList){
    int len = line.length();
    string temp;
    vector<string> output;
    for(int i = 0; i < len; i++){
        if(isDelim(line[i], delimList)){
            if(temp.length() > 0)
                output.push_back(temp);
            temp.clear();
        }
        else
            temp += line[i];
    }
    if(temp.length() > 0)
        output.push_back(temp);
    return output;
}

int main(){
    string    input;
    string    delimList;
    vector<string>    inputSplit;
    
    cout << "Type something: ";
    getline(cin, input);
    cout << "What are your delimiters? ";
    getline(cin, delimList);
    
    inputSplit = split(input, delimList);
    
    for(int i = 0; i < inputSplit.size(); i++)
        cout << inputSplit[i] << endl;
        
    return 0;
}
commented: Do not give entire answers to a problem -1
commented: We are not a coding service. Do not post working answers -3

No, "here you go" is not acceptable. One, you've probably overshot the OPs progress in his course by a couple miles and two, that is not a way to teach anyone anything -- how does handing him something help him learn?

We are not allowed to use
#include <vector>

Just iostream, cstring and cstdio!

Can someone post an example of a single string then separating it into 3 multiple strings?

Im just learning C++ so please simplify it :)
Thank You.

Sorry everyone!

OP, in the future, please explicitly state that your question/problem is coursework related and a brief note about your level of experience.

@jonsca, at the time, I honestly didn't think about it being a homework problem. I "usually" never give out free answers. My apologies.

Well, there is a simple way to do it if you are restricted to iostream, cstring and cstdio.

A simple modification of L7Sqr's code to use C-strings instead:

std::cout << "First names: ";
    char token[100];
    std::cin.getline ( token, ',' );
    std::cout << "Read token: " << token << std::endl;
    std::cin.getline ( token, ',' );
    std::cout << "Read token: " << token << std::endl;
    std::cin.getline ( token );
    std::cout << "Read token: " << token << std::endl;

Sorry everyone!

It's all good.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.