How to separate 2 inputs(with spaces) into 2 arrays in c++
I tried to do one input like this but adding another input is very long: #include <iostream> using namespace std; bool isNumber(string str) { int i = 0; if (str[0] == '-') { i++; } for (; i < str.length(); i++) { if (!isdigit(str[i])) { return false; } } return true; } int findLengthOfInput(string input) { int n = 1; int k = 0; while (input[k] == ' ') { k++; } string word; for (; k < input.length(); k++) { if (input[k] == ' ') { if (!isNumber(word)) { continue; } while (input[k] == ' ') { k++; } n++; word = ""; } word += input[k]; } if (!isNumber(word)) { n = 0; } return 0; } int main() { // First input string input; int n = 0; do { cout << "Enter the first input? "; cin >> input; string input2; getline(cin, input2); input = input + input2; // Find number of items in input n = findLengthOfInput(input); } while (n != 2); // Separate input int j = 0; string arr[2]; while (input[j] == ' ') { j++; } for (int i = 0; i < 2; i++) { string word = ""; for (; j < input.length(); j++) { if (input[j] == ' ') { while (input[j] == ' ') { j++; } break; } word += input[j]; } arr[i] = word; } // Some code }