-1

I have an array of strings of "first last" names. On the file i have the names written as:

Abe Adams.

John Adams.

John Doe.

Sarah Smith.

I have stored these into a string, but without the period. What I want to do is separate first and last names into two different strings. Here is my code but it doesnt work? What am i doing wrong?

{
 int n = MAX;

 for (int i = 0; i < n; i++){
     last[i] = substr(' ',',');


     for (int i = 0; i < n; i++){
        first[i] = getline(cin,x[i],' ');
     }

      return;
 }
Anthony40k
  • 29
  • 3

2 Answers2

2

Assuming that you are reading your input from stdin, one (trivial) way to do this would be to read directly from cin as follows:

std::vector<std::string> first_names, last_names;

while (std::cin)
{
    std::string first_name, last_name;
    std::cin >> first_name >> last_name;
    first_names.push_back(first_name);
    last_names.push_back(last_name);
}

This works given the very simple format of your input, but anything more complex might not be so straightforward. It would be better as a general rule to read each line from the input into a string and process it there:

std::string line;
while (std::getline(std::cin, line))
{
    // Do some stuff here with 'line', e.g.
    size_t space_pos = line.find_first_of(' ');
    std::string first_name = line.substr(0, space_pos);
    std::string last_name = line.substr(space_pos + 1);
}

This will give you more options such as using a string tokeniser or a regular expression pattern matcher to extract based on more complex criteria.

Naturally, if you aren't reading your name pairs from stdin, but from an array or vector instead, you can simply iterate over the collection and substitute line with the target of the iterator.

Component 10
  • 10,247
  • 7
  • 47
  • 64
  • The question is *"What I want to do is separate first and last names into two different strings"* Can you explain in what way it does not answer the question? I've read it through a couple of times and I don't see how this doesn't solve the OPs problem. – Component 10 Dec 10 '14 at 10:24
  • I did read the question, did you? Do you see this bit: `first[i] = getline(cin,x[i],' ');`? Do you know what it means? How is it that you assume that the OP *likely* doesn't use `cin` when it's there in the code. I still don't understand how my answer is completely invalid. Why don't you provide an answer yourself if you have such insight? – Component 10 Dec 10 '14 at 10:30
  • I *am* providing an answer. – AStopher Dec 10 '14 at 10:38
  • @Component10 Also note: [`while (!std::cin.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong)! – πάντα ῥεῖ Dec 10 '14 at 10:44
  • @πάντα ῥεῖ - Thanks for your constructive comment, I've made that change. – Component 10 Dec 10 '14 at 10:47
  • 1
    Ok- I admit I was wrong. My answer now has hardly any resemblance to your answer (apart from the parts I usually use), and I apologise. – AStopher Dec 10 '14 at 11:13
  • 1
    @cybermonkey: Thanks. Apology accepted. No hard feelings :) – Component 10 Dec 10 '14 at 11:14
0

You have a few errors in your code:

  • You are missing a closing brace
  • substr does not take char as an argument

You can do this:

    std::vector<std::string> fns, lns;
    std::string fn, ln;

    bool hasinput = true;
    std::string inp;

    while (hasinput)
    {
        std::getline(std::cin, inp);
        if (inp.empty())
        {
            hasinput = false;
            break;
        }
        else
        {
            fn, ln = inp;
            fns.push_back(fn);
            lns.push_back(fn);
        }
    }

Don't forget these #includes:

#include <iostream>
#include <string>
#include <vector>
AStopher
  • 4,207
  • 11
  • 50
  • 75
  • `_tmain(int argc, _TCHAR* argv[])` isn't standard c++. `using namespace std;` is strongly discouraged. – πάντα ῥεῖ Dec 10 '14 at 11:12
  • @πάνταῥεῖ I never used `while(!cin.eof())` (in this answer). – AStopher Dec 10 '14 at 11:14
  • you've used it in your [1st try](http://stackoverflow.com/posts/27399171/revisions). I removed this critique from my comment meanwhile. – πάντα ῥεῖ Dec 10 '14 at 11:14
  • @πάνταῥεῖ True. I revised my answer to reflect your comments and also removed the Windows-specific example. – AStopher Dec 10 '14 at 11:17
  • You should also note, that deleting a downvoted answer and giving another one, doesn't help much to prevent you being banned from answering. The deleted answer still counts. – πάντα ῥεῖ Dec 10 '14 at 11:17
  • @πάνταῥεῖ Yes, I know that. The same with questions. As long as I don't keep giving poor answers like my first try on this question I should be fine. – AStopher Dec 10 '14 at 11:19