SHARE
TWEET

Untitled

a guest Jan 19th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. using namespace std;
  5.  
  6. int findLength(string &str);
  7. void initializeArray(int arr[], string &str);
  8.  
  9. int main()
  10. {
  11.     cout << "enter the firs arr: ";
  12.     string str;
  13.     getline(cin, str);
  14.     int arr1[findLength(str)] = {};
  15.  
  16.     initializeArray(arr1, str);
  17.  
  18.     cout << "enter the second arr: ";
  19.     getline(cin, str);
  20.     int arr2[findLength(str)] = {};
  21.  
  22.     initializeArray(arr2, str);
  23.  
  24.     cout << "arr1 elements:" << endl;
  25.     for(int e : arr1)
  26.         cout << e << endl;
  27.  
  28.     cout << "arr2 elements:" << endl;
  29.     for(int e : arr2)
  30.         cout << e << endl;
  31. }
  32.  
  33. int findLength(string &str)
  34. {
  35.     int n = 0;
  36.  
  37.     for(char e : str)
  38.         if(e == ' ')
  39.             n++;
  40.  
  41.     return ++n;
  42. }
  43.  
  44. void initializeArray(int arr[], string &str)
  45. {
  46.     bool sInput = false;
  47.     int index = 0;
  48.  
  49.     while(!sInput)
  50.     {
  51.         istringstream inputStream(str);
  52.  
  53.         ostringstream outputStream;
  54.  
  55.         sInput = true;
  56.         while(inputStream)
  57.         {
  58.             if(inputStream >> arr[index])
  59.                 index++;
  60.             else
  61.             {
  62.                 inputStream.clear();
  63.                 string unparsed;
  64.                 inputStream >> unparsed;
  65.  
  66.                 if(!unparsed.empty())
  67.                 {
  68.                     outputStream << unparsed << " ";
  69.                     sInput = false;
  70.                 }
  71.             }
  72.         }
  73.  
  74.         if(!sInput)
  75.         {
  76.             cout << "unparsed numbers: " << outputStream.str() << "enter again: ";
  77.  
  78.             getline(cin, str);
  79.         }
  80.     }
  81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top