0

Hello so I've seen some other posts about it but they never seem to solve my issue, or I just can't understand it with my peanut sized brain. If I input something, and push it back to the vector, it would always have some stupidly absurd number(s)

For those who want context or in case it's outside of what I think is the problem, here's the full code:

#include <iostream>
#include <string>
#include <array>
#include <algorithm>
#include <vector>

using namespace std;

void stringToVec(string input, vector<int> output)
{
    int e = 0;
    string temp;

    //cout << "size: " << input.size() << '\n';

    for(int i = 0; i < input.size(); i++)
    {
        if(input[i] != ' ')
        {
            temp += input[i];
        }
        else if(input[i] == ' ')
        {
            int num = stoi(temp);

            output.push_back(num);
            temp = ""; // clear temp
        }
    }
}

int main()
{
    int a, b;
    string numsIn1, numsIn2;

    cin >> a;
    cin >> ws;
    getline(cin, numsIn1);

    numsIn1 += ' ';
    int nums1[a];
    // =======================
    cin >> b;
    cin >> ws;
    getline(cin, numsIn2);

    numsIn2 += ' ';
    int nums2[b];
    // =======================
    vector<int> output;

    stringToVec(numsIn1, output);
    stringToVec(numsIn2, output);

    int e = 0;

    for(int i = 0; i < a; i++)
    {
        output.push_back(nums1[i]);
    }
    for(int i = 0; i < b; i++)
    {
        output.push_back(nums2[i]);
    }

    sort(output.begin(), output.end());

    //int dyfslashj[3] = {3, 2, 1};

    for(int i = 0; i < a + b; i++)
    {
        cout << output[i] << ' ';
    }

    return 0;
}

Here's the line thats most likely to be the problem. For example if I input the number "3 4 2" the vector would be like "5234523452345 32452 34523", or some absurd value

void stringToVec(string input, vector<int> output)
{
    int e = 0;
    string temp;

    //cout << "size: " << input.size() << '\n';

    for(int i = 0; i < input.size(); i++)
    {
        if(input[i] != ' ')
        {
            temp += input[i];
        }
        else if(input[i] == ' ')
        {
            int num = stoi(temp);

            output.push_back(num);
            temp = ""; // clear temp
        }
    }
}
  • 3
    you pass the vector to `StringToVec` by value instead of by reference, so operations you perform on the vector inside the function don't affect it. just pass by reference (`&`) – Gal Aug 03 '21 at 07:30
  • not related to your question, but you can also read numbers directly from a stream with `cin >> myIntVariable` you can put that in a loop to directly fill the vector without `stoi`. This also takes care of the whitespaces in between. – Timo Aug 03 '21 at 07:33
  • 1
    `int nums1[a];` is not standard c++. Use `std::vector` for dynamically sized arrays. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Aug 03 '21 at 07:54

1 Answers1

-1

maybe there are something wrong with your "cin". I can see you used cin and getline. But getline will read at the first line.No matter it have been "cin".you can try to use some strings as parameter to the function to see whether it's correct or not

mainjay
  • 1
  • 1
  • Are you talking about the cin and getline conflict? I've done research on that and turns ``cin >> ws;`` is a solution, however I am a pea brain like I said so you can correct me if I'm wrong in any way. – Blueberry Tech Aug 03 '21 at 08:07