0

i have to write a program which read from the keyboard a line of numbers and save them into an array, numbers have to be written just in a line, i wrote this but doesn`t work because of an infinite loop, any suggestion?

int main() {

    int numCasos = 0, contNumCasos = 0, numElem = 0;
    string aux;
    cout << "Number of cases: " << endl;
    cin >> numCasos;
    while (contNumCasos < numCasos) {

        cout << "Number of elements: " << endl;
        cin >> numElem;
        cout << "Enter the Elements separated by space: " << endl;
        cin.ignore();
        vector.cont = 0;
        int i = 0;
        while ((vector.cont < numElem) && getline(cin,aux,' ')){
            vector.v[i] = stoi(aux);
            vector.cont++;
            i++;
        }
    }

    cout << sumaBuenos(vector) << endl;
    cin.ignore();
    system("pause");
    return 0;
}

An example:
console: Number of elements:
user: 4
console: Enter the Elements separated by space:
user: 2 43 65 56
--this has to be the vector
-- vector.v[0] = 2
-- vector.v[1] = 43
-- vector.v[2] = 65
-- vector.v[3] = 56

mac
  • 9,885
  • 4
  • 36
  • 51
kriptor
  • 17
  • 1
  • 1
  • 5
  • Seems like a pretty long-winded substitute for `std:vector input{std::istream_iterator(std::cin), std::istream_iterator()};` – Jerry Coffin Nov 28 '15 at 17:58
  • Search the internet for possible duplicates: `stackoverflow c++ read file vector space separated`. – Thomas Matthews Nov 28 '15 at 18:03
  • i searched, but the solutions they gave is to read from a file, i need to read from the keyboard – kriptor Nov 28 '15 at 18:17
  • Interesting fun fact: both `cin` and a file stream behave the same way. A function that reads from a file can also read from `cin` if you replace the file with `cin`. – user4581301 Nov 28 '15 at 18:57

3 Answers3

2

if you know how many numbers you have to read there is a simpler way:

int n;
cin>>n; // how many numbers;
vector<int> v(n);
for ( int i=0; i<n; ++i ){
    cin>>v[i];
}
Saeid
  • 4,147
  • 7
  • 27
  • 43
0

With a modification it works.

int main() {

    int numCasos = 0, contNumCasos = 0, numElem = 0;
    string aux;
    cout << "Numero de casos: " << endl;
    cin >> numCasos;
    while (contNumCasos < numCasos) {

        cout << "Numero de elementos: " << endl;
        cin >> numElem;
        cout << "Ingrese los elementos separados por espacios: " << endl;
        cin.ignore();
        vector.cont = 0;
        getline(cin, aux);
        istringstream iss(aux);
        for (int i = 0;i < numElem;i++) {
            iss >> aux;
            vector.v[i] = stoi(aux);
            vector.cont++;
        }
        cout << sumaBuenos(vector) << endl;
        contNumCasos++;
    }

    system("pause");
    return 0;
}

to someone who have the same problem.

kriptor
  • 17
  • 1
  • 1
  • 5
0
#include<iostream>
using namespace std; 

int main() 
{   
    int n;
    cout << "input numbers" << endl;
    cin >> n; 

    vector<int> v(n);

    for ( int i=0; i<n; ++i )
    {

        cin >>v[i];

    } 
    return 0; 

} 
Farbod Ahmadian
  • 728
  • 6
  • 18