Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- using namespace std;
- int findLength(string &str);
- void initializeArray(int arr[], string &str);
- int main()
- {
- cout << "enter the firs arr: ";
- string str;
- getline(cin, str);
- int arr1[findLength(str)] = {};
- initializeArray(arr1, str);
- cout << "enter the second arr: ";
- getline(cin, str);
- int arr2[findLength(str)] = {};
- initializeArray(arr2, str);
- cout << "arr1 elements:" << endl;
- for(int e : arr1)
- cout << e << endl;
- cout << "arr2 elements:" << endl;
- for(int e : arr2)
- cout << e << endl;
- }
- int findLength(string &str)
- {
- int n = 0;
- for(char e : str)
- if(e == ' ')
- n++;
- return ++n;
- }
- void initializeArray(int arr[], string &str)
- {
- bool sInput = false;
- int index = 0;
- while(!sInput)
- {
- istringstream inputStream(str);
- ostringstream outputStream;
- sInput = true;
- while(inputStream)
- {
- if(inputStream >> arr[index])
- index++;
- else
- {
- inputStream.clear();
- string unparsed;
- inputStream >> unparsed;
- if(!unparsed.empty())
- {
- outputStream << unparsed << " ";
- sInput = false;
- }
- }
- }
- if(!sInput)
- {
- cout << "unparsed numbers: " << outputStream.str() << "enter again: ";
- getline(cin, str);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement