-1

I got this code that I'm working with. I need to take in inputs from command line and work with that input.

For example, I could have the input:

a 3 b 2 b 1 a 1 a 4 b 2

which will give the output:

1 3 4 1 2 2

My problem is that I can't use another input than of size 6 (or 12).

If I use the input

a 3 a 2 a 3

I will get the output:

2 3 3 3 

but should get:

2 3 3

How can I have unknown size as an input without getting in trouble?

I am trying to solve the following question:

Read the datasets and write them into cout in the following order: first by dataset (first a, then b) and then by value. Example: input: a 3 b 2 b 1 a 1 a 4 b 2 output: 1 3 4 1 2 2

#include <iostream>
#include <math.h>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
#include <iomanip>
#include <vector>
using namespace std;

/*
input: a 3 b 2 b 1 a 1 a 4 b 2
output: 1 3 4 1 2 2
*/

//void insert_left

//void insert_right

//void Update

int main()
{
    string my_vec_str;
    double x;

    vector<string> vect_string;
    vector<int> vect_int;

    bool go_on = true;

    while (go_on)
    {
        cin >> my_vec_str;
        cin >> x;

        vect_string.push_back(my_vec_str);
        vect_int.push_back(x);

        if (cin.fail())
        {
            go_on = false;
        }

        if (vect_string.size() == 6 && vect_int.size() == 6)
        {
            go_on = false;
        }
    }

    vector<int> vect_a;
    vector<int> vect_b;

    for (int i = 0; i < vect_string.size(); i++)
    {
        if (vect_string[i] == "a")
        {
            vect_a.push_back(vect_int[i]);
        }
        if (vect_string[i] == "b")
        {
            vect_b.push_back(vect_int[i]);
        }
    }

    sort(vect_a.begin(), vect_a.end());
    sort(vect_b.begin(), vect_b.end());

    vector<int> vect_c;

    for (int i = 0; i < vect_a.size(); i++)
    {
        vect_c.push_back(vect_a[i]);
    }

    for (int i = 0; i < vect_b.size(); i++)
    {
        vect_c.push_back(vect_b[i]);
    }

    for (auto &&i : vect_c)
    {
        cout << i << ' ';
    }

    return 0;
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • What happens after the last piece of data is sent? Do you send something like "END" or eof or close `cin`? – scohe001 Oct 05 '20 at 19:48
  • No that is the problem. I don't know how to end the while loop without doing it myself. – Grétar Már Oct 05 '20 at 19:54
  • This doesn't address the question, but `vect_c` isn't needed. Just use two loops, one to write the contents of `vect_a` and one to write the contents of `vect_b`. – Pete Becker Oct 05 '20 at 21:04
  • Ahh ofc, good point! thanks! I also don't know how to use "END" or eof. I'm not sure how to sense when I can let the computer know when the last piece of data has been sent. – Grétar Már Oct 05 '20 at 21:34

1 Answers1

1

You can use a combination of std::getline with std::stringstream to parse the input, this allows you to use a variable size input without having to worry about it:

Live demo

#include <sstream>

//...

std::string my_vec_str;
char type; //to store type 'a' or 'b'
int value; //to store int value

std::vector<int> vect_a;
std::vector<int> vect_b;

std::getline(std::cin, my_vec_str); //parse the entirety of stdin
std::stringstream ss(my_vec_str); //convert it to a stringstream

while (ss >> type >> value) //parse type and value
{
    if (type == 'a')
    {
        vect_a.push_back(value);
    }
    if (type == 'b')
        vect_b.push_back(value);
}
//from here on it's the same

Input:

a 3 a 2 a 3

Or:

a 3 a 2 b 3 a b a //invalid inputs are not parsed

Output:

2 3 3

Note that I'm not using using namespace std; and there are good reasons for that.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • There's no need for the stringstream. Just read `type` and `value` directly from `std:::cin`. The `while` loop will exit when the input ends. That allows for input that's more than one line long. (I don't see a requirement here that the input be entirely on one line) – Pete Becker Oct 05 '20 at 22:17
  • @PeteBecker, I assumed so by the way the OP posted the sample inputs, anyway a `while(cin >> type >> value)` will only break with a bad input, which is fine, I guess. – anastaciu Oct 06 '20 at 07:52