-2

My program receives input from the user, in this exact form: string, integer. An example would be: Brady, 12. I need to split the string at the comma, and store the first part into a vector of strings, and the 2nd part into a vector of integers. How do I go about this? (Sorry, I'm very new to programming)

This is what my code currently looks like:

while(true) {

    cout << "Enter a data point (-1 to stop input): " << endl;
    getline(cin, dataPoint);


    if (dataPoint == "-1") { //Ends Program
        break;
    }
    if (dataPoint.find(",") == std::string::npos) { 
        cout << "Error: No comma in string." <<endl;
    }

}
Sumeet
  • 1
  • 1
  • 2
    "Write my code for me" questions aren't appropriate to Stack Overflow. If you make an effort, and it doesn't work, you can provide a [MCVE] for us to *help* with, but we can't teach you C++ from scratch, and writing your code for you will teach you little or nothing. – ShadowRanger Sep 03 '21 at 15:41
  • 1
    If you don't know how to solve a problem you should do it step by step: **1.** [Parse (split) a string in C++ using string delimiter (standard C++)](https://stackoverflow.com/questions/14265581) **2.** [How can I convert a std::string to int?](https://stackoverflow.com/questions/7663709) and then try to bring that together. If you at some point have a problem there, show the code you have and explain with what part you have a problem with. Or if you have a functional code you could also show that and ask how to improve it. – t.niese Sep 03 '21 at 15:42
  • 1
    Welcome to Stack Overflow! Generally, when asking a question you should always show an effort(such as code snippet for us to easily reproduce the case). Currently, your question is some of type "write code for me", which we don't do in stack overflow. – Karen Baghdasaryan Sep 03 '21 at 15:43
  • Search the internet for "C++ read comma separated values". – Thomas Matthews Sep 03 '21 at 16:18
  • You started out in the right directory (or a reasonable direction), now just follow through with [std::basic_string::substr](https://en.cppreference.com/w/cpp/string/basic_string/substr) and don't forget to keep a counter of the characters consumed so you can start your next substring at the beginning of your 2nd token. – David C. Rankin Sep 03 '21 at 17:24
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 08 '21 at 06:25

2 Answers2

0

We wouldn't normally write your code for you, but I'm going to offer a few hints.

First, storing the data the way you're going to do is probably the wrong approach, unless it's required for your homework. I'd keep it related:

class Person {
    ... various methods
private:
     std::string name;
     int age;
}

I would parse the data into one of these and store THAT into a std::vector<Person>. That keeps your data together.

As for splitting the string:

size_t pos = str.find(",");
if (pos == string::npos) {
    ... no comma found
}
else {
    string name = str.substr(0, pos);
    string ageStr = str.substr(pos + 1);
}

There's more you have to do, of course, but that's how you can divide the string into two pieces.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
0

Here is the solution:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    cout << "Enter a data point (-1 to stop input): " << endl;
    string input;
    getline(cin, input);
    if (input == "-1") return 0;
    int comma = input.find(","); //find separator
    string name = input.substr(0, comma); //find name and nuber using this number
    string snumber = input.substr(comma + 2); // comma itself and space
    cout << "The name is: " << name << " The number is: " << snumber << endl;
    int number = stoi(snumber); //switch string to int

    vector<string> names;
    vector<int> numbers;

    names.push_back(name); // add to vector
    numbers.push_back(number);

    return 0;
}
Mike
  • 519
  • 1
  • 4
  • 10