3

I'm working on a school project and I'm a little stuck. I need to get input SETS if integers using cin (so I type in the numbers OR can pipe in from command prompt) in any of the following formats:

3,4

2,7

7,1

OR option 2,

3,4 2,7

7,1

OR option 3,

3,4 2,7 7,1

There might also be a space after the ,, like 3, 4 2, 7 7, 1

Using this info, I must put the first number of the set into 1 vector and the second number (after the ,) into the second vector.

Currently, what I have below does almost exactly what I need it to do, however when using option 2 or 3 reading from a file, when std::stoi() reaches a space, I get a debug error (abort() has been called)

I've tried using stringstream but I can't seem to use it correctly for what I need.

What can I do to fix this?

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

using namespace std;

int main() {

    string input;

    // Create the 2 vectors
    vector<int> inputVector;
    vector<int> inputVector2;

    // Read until end of file OR ^Z
    while (cin >> input) {
        
        // Grab the first digit, use stoi to turn it into an int
        inputVector.push_back(stoi(input));

        // Use substr and find to find the second string and turn it into a string as well.
        inputVector2.push_back(stoi(input.substr(input.find(',') + 1, input.length())));
    }

    // Print out both of the vectors to see if they were filled correctly...
    cout << "Vector 1 Contains..." << endl;
    for ( int i = 0; i < inputVector.size(); i++) {
        cout << inputVector[i] << ", ";
    }
    cout << endl;

    cout << endl << "Vector 2 Contains..." << endl;
    for ( int i = 0; i < inputVector2.size(); i++) {
        cout << inputVector2[i] << ", ";
    }
    cout << endl;

}
Community
  • 1
  • 1
KappaPride
  • 45
  • 7
  • Please take [the tour](http://stackoverflow.com/tour) and read the [help page](http://stackoverflow.com/help). Here is [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Feb 09 '17 at 02:00
  • 1
    Can it be `3, 7` (with a space), or must it always be `3,7` with no space? – Justin Feb 09 '17 at 02:02
  • @Justin Either or. There can be a space as long as one integer of the set goes into vector1 and the other goes into vector2. It should be able to handle any number of sets – KappaPride Feb 09 '17 at 02:04
  • @KappaPride meaning, what are requirements for the input? Say, if it's `3, 7` in the input, then `cin >> input` will read only `3,`, resulting in second `stoi` being called on an empty string – yeputons Feb 09 '17 at 02:04

1 Answers1

5

cin already ignores whitespace, so we need to also ignore commas. The easiest way to do this is simply store the comma in an unused char:

int a, b;
char comma;

cin >> a >> comma >> b;

That would parse a single #, # with optional whitespace between any elements.

Then, to read a bunch of such comma separated values, you could do something like this:

int a, b;
char comma;

while (cin >> a >> comma >> b) {
    inputVector.push_back(a);
    inputVector2.push_back(b);
}

However, your two vectors would be better replaced by a single vector of pair<int, int>:

#include <utility> // for std::pair

...

vector<pair<int, int>> inputVector;

...

while (cin >> a >> comma >> b) {
    inputVector.push_back(pair<int, int>{ a, b });
}

DEMO

Justin
  • 24,288
  • 12
  • 92
  • 142
  • Thank you SO MUCH! I was clearly way over thinking it and forgetting the basics! Your solution worked perfectly! – KappaPride Feb 09 '17 at 02:24