0

I am working on program that would calculate with matrixes but i am not sure what is best way how to read matrix line by line from command line.

My goal is this:

Please enter number of lines:
2
Please enter line 1/2:
1 4 5 2 
Please enter line 2/2:
1 5 7 8

At the end of this i would like to have array or vector of numbers 1,4,5,2,1,5,7,8.

This is my code:

vector<string>matrix;
string input;
int nrows;
cout << "Enter number of rows:" << endl;
cin >> nrows;
getline(cin, input);
for (int i = 1; i <= nrows; i++) {
    cout << "Enter line " << i << "/" << nrows << endl;
    getline(cin, input);
    matrix.push_back(input);
}
for (int i = 0; i < matrix.size();i++){
    cout << matrix.at(i)<<endl;
}

This reads whole line and save it into vector of string and there is much to do to separate just numbers. Is there any way how could I load only numbers in the line ? So for example for the line:

1 a 3 2 4sdsd

I would get numbers 1,3,2,4 ?

Thank for any help.

Tim
  • 38,263
  • 17
  • 115
  • 131
Pastx
  • 487
  • 2
  • 6
  • 19

3 Answers3

3
string process(const string& input)  // Removes all characters except <space> and digits [0-9]
{
    string ret;
    for(int i=0; i<(int)input.size(); i++)
    {
        if(input[i]==' '||(input[i]>='0'&&input[i]<='9'))
            ret+=input[i];
    }
    return ret;
}

int main()
{
    int nrows;
    string input;
    cout<<"Enter number of rows - ";
    cin>>nrows;
    cin.get();  // Take the remaining <Enter>
    vector<vector<int> > matrix(nrows);  // A 2-D vector representing the matrix
    for(int i=0; i<(int)matrix.size(); i++)
    {
        cout<<"Please enter line "<<i+1<<"/"<<nrows<<" -: \n";
        getline(cin,input);
        stringstream ss(process(input));
        int num;
        while(ss>>num)
        {
            matrix[i].push_back(num);
        }
    }

    for(int i=0; i<(int)matrix.size(); i++)  // Display the matrix
    {
        for(int j=0; j<(int)matrix[i].size(); j++)
        {
            cout<<matrix[i][j]<<" ";
        }
        cout<<"\n";
    }
}
Anmol Singh Jaggi
  • 7,318
  • 1
  • 31
  • 65
2

I would use a 2D vector. Also, ask the user for number of columns:

vector<vector<int>> matrix;

int nrows, ncols;
cout << "Enter number of rows:" << endl;
cin >> nrows;
cout << "Enter number of columns:" << endl;
cin >> ncols;
matrix.resize(nrows);
for (int i = 0; i < nrows; i++) {
    cout << "Enter line " << (i+1) << "/" << nrows << endl;
    int tmp;
    while (matrix[i].size() < ncols) {
        while (!(cin >> tmp)) { // Not a number. Clear cin
            cin.clear();
            cin.ignore(1);
        }
        matrix[i].push_back(tmp);
    }
}
Johnny Mopp
  • 10,608
  • 4
  • 33
  • 58
1

One way to do it:

// only allow digits and spaces
string removeNonNumbers(const string& s) {
  stringstream ss;
  for(int i=0; i<s.length(); ++i) {
    if(isdigit(s[i]) || ' ' == s[i])
      ss << s[i];
  }
  return ss.str();
}

vector<int> splitToInts(const string& s) {
  vector<int> ret;
  stringstream ssin(s);
  while (ssin.good()){
      string tmp;
      ssin >> tmp;
      ret.push_back(atoi(tmp.c_str()));
  }
  return ret;
}

To use it, do this when reading input in your getline loop:

vector<int> numbers = splitToInts( removeNonNumbers(input) ); 
tpatja
  • 690
  • 4
  • 11