The task is to read an integer array from a user and then count sort it. I used vector to take input of the array but it only stops reading integers after I enter non-integer symbol when it has to do it after the last integer. For example, if I enter (5 4 3 2 1) it does not proceed further but keeps expecting input from a user. Also the sorted array must look like "1 2 3 4 5) but I have 0 at the beginning of it (0 1 2 3 4 5).
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
int main(){
vector<int> arr;
int x;
int j = 0;
while(cin.good()==1){
cin>>x;
arr.push_back(x);
j++;
}
cin.clear();
int count_array[j]={0};
int s=0;
int new_array[j];
int i;
for(i=0; i<j; i++)
count_array[arr[i]]++;
for(i=0; i<j; i++){
count_array[i] = count_array[i] + s;
s=count_array[i];
}
for(i=0;i<j;i++){
new_array[count_array[arr[i]]]=arr[i];
count_array[arr[i]]--;
}
for(i=1;i<=j;i++)
{
cout<<new_array[i]<<" ";
}
return 0;
}