-3

hi I'm trying to read that input

2
2
9 97
8 56
3
1 18 6
16 42 100
25 16 17

I can't get the numbers 9, 97, 8, 56 and store them in a vector of

here is my attempt

using namespace std;

int main() {

    int test;

    cin >> test;

    while (test--)
    {
        int ss_i;
        cin >> ss_i;

        vector<int> A(ss_i*ss_i);



    }
    return 0;
}
Andre
  • 51
  • 9
  • why negating my question ?! – Andre Dec 08 '16 at 11:27
  • 5
    That's completely nonsensical code. You're just creating a new vector instance in each iteration: `vector A(ss_i*ss_i);` (which can be considered a NOP). – πάντα ῥεῖ Dec 08 '16 at 11:27
  • Why I downvoted your question? Your research efforts seem to be extremely low: https://www.google.de/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%2B%2B+read+a+matrix+from+cin The DV button tooltip already tells this to you. – πάντα ῥεῖ Dec 08 '16 at 11:34

1 Answers1

0

You'll need to fill a string and then access the integers through a std::istringstream. also consider push_back to store those integers into a vector:

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

int main(){

    int size{ 16 };

    std::vector<int> numbers;
    numbers.reserve(size); //reserve memory


    for (int i = 0; i < size;){
        std::string ss_i; //number(s) to push_back


        std::cout << "Type in " << size - i << " more integers: ";
        std::getline(std::cin, ss_i);

        std::istringstream stringstream(ss_i); //load into stream

        int n;
        while (stringstream >> n){
            i++;
            numbers.push_back(n); //add to the vector
        }
    }

    std::cout << "The vector contains:\n";
    for (auto i : numbers){
        std::cout << i << ' ';
    }
    return 0;
}

Example Run:

Type in 16 more integers: 2
Type in 15 more integers: 2
Type in 14 more integers: 9 97
Type in 12 more integers: 8 56
Type in 10 more integers: 3
Type in 9 more integers: 1
Type in 8 more integers: 18 6
Type in 6 more integers: 16 42
Type in 4 more integers: 100 25 16 17
The vector contains:
2 2 9 97 8 56 3 1 18 6 16 42 100 25 16 17 
Stack Danny
  • 7,754
  • 2
  • 26
  • 55