Guest User

string_and_int_Stringstreams

a guest
Jan 19th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include<cstdlib>
  4. #include <cctype>
  5. #include<cctype>
  6.  
  7. using namespace std;
  8. const int maxSize = 1000;
  9.  
  10. int findLength(string &str);
  11. void initializeArray(int arr[], int sizeArr, string &str);
  12. bool AreOnlyNumbers(string strNum);
  13.  
  14. int main()
  15. {
  16.     cout << "enter the firs arr: ";
  17.     string str;
  18.     getline(cin, str);
  19.     int arr1[maxSize] = {};
  20.     int maxFilledIndex1 = findLength(str);
  21.     initializeArray(arr1, maxFilledIndex1, str);
  22.  
  23.     cout << "enter the second arr: ";
  24.     getline(cin, str);
  25.     int arr2[maxSize] = {};
  26.     int maxFilledIndex2 = findLength(str);
  27.     initializeArray(arr2, maxFilledIndex2, str);
  28.  
  29.     cout << "arr1 elements: ";
  30.     for (int i = 0; i < maxFilledIndex1; i++)
  31.     {
  32.         cout << arr1[i] << " ";
  33.     }
  34.     cout << endl;
  35.  
  36.  
  37.     cout << "arr2 elements: ";
  38.     for (int i = 0; i < maxFilledIndex2; i++)
  39.     {
  40.         cout << arr2[i] << " ";
  41.     }
  42.     cout << endl;
  43. }
  44.  
  45. int findLength(string &str)
  46. {
  47.     int n = 0;
  48.  
  49.     for (char e : str)
  50.         if (e == ' ')
  51.             n++;
  52.  
  53.     return ++n;
  54. }
  55.  
  56. void initializeArray(int arr[], int sizeArr, string &str)
  57. {
  58.     bool sInput = false;
  59.     int index = 0;
  60.  
  61.     while (!sInput)
  62.     {
  63.         istringstream inputStream(str);
  64.  
  65.         ostringstream outputStream;
  66.  
  67.         sInput = true;
  68.  
  69.         while (inputStream)
  70.         {
  71.             string strNum;
  72.             inputStream >> strNum;
  73.  
  74.             if (AreOnlyNumbers(strNum))
  75.             {
  76.                 int num = atoi(strNum.c_str());
  77.                 arr[index] = num;
  78.                 index++;
  79.             }
  80.             else
  81.             {
  82.                 //inputStream.clear();
  83.                 //string unparsed;
  84.                 //inputStream >> unparsed;             
  85.  
  86.                 if (!strNum.empty())
  87.                 {
  88.                     outputStream << strNum << " ";
  89.                     sInput = false;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         if (!sInput)
  95.         {
  96.             cout << "unparsed numbers: " << outputStream.str() << "enter again: ";
  97.  
  98.             getline(cin, str);
  99.         }
  100.     }
  101. }
  102.  
  103. bool AreOnlyNumbers(string strNum)
  104. {
  105.     if (strNum.empty())
  106.     {
  107.         return false;
  108.     }
  109.     for (int i = 0; i < strNum.size(); i++)
  110.     {
  111.         if (isalpha(strNum[i]))
  112.         {
  113.             return false;
  114.         }
  115.     }
  116.     return true;
  117. }
Add Comment
Please, Sign In to add comment