-1
vector<int> var;
int numb;
cout<<"Enter number: ";
while (cin>>numb) {
    if (cin.get()==char(32)) {
        var.push_back(numb);
        shellsort(var);
        for (int i=0; i<var.size(); i++) {
            cout<<var[i]<<" ";
        }
    } else if (cin.get()=='\n') {
            break;
    }
}   

I used ascii code 32 to read space and a sort function. Loop the value to show current list. The problem is the current sorted list wont show during input of value. What to do?

J. V. A.
  • 529
  • 3
  • 11
CJ Canlas
  • 31
  • 1
  • 8
  • 2
    Notes: 1. Why do you use magic number `32` instead of portable "space" `' '`? 2. Your usage of `cin.get()` should cause some troubles. You should call `cin.get()` only once after each call of `cin>>numb`, assign its return value to an variable and use the assigned value for conditions. – MikeCAT Feb 24 '16 at 05:34
  • If you want to enter many numbers separated by space in one go, you should use `getline` and then parse the line for numbers. `cin<<` [fails to read input with space](http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces) – Atul Feb 24 '16 at 05:53
  • You also might want to `cout` a `"\n"` or `std::flush` at some point too. – kfsone Feb 24 '16 at 06:31

1 Answers1

0

You are calling, cin.get(), twice unnecessarily, fix that, and you are good to go.

Here is the fixed code:

vector<int> var;
int numb;
cout<<"Enter number: ";
while(cin>>numb)
{
   char c = cin.get();
   if(c==char(32))
   {
    var.push_back(numb);
    shellsort(var);
    for(int i=0;i<var.size();i++)
    {
     cout<<var[i]<<" ";
    }
    cout<<endl;
   }
   else if(c=='\n')
   {
       break;
   }
}
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • Going to try this code now, thanks for the answer :) – CJ Canlas Feb 25 '16 at 12:03
  • @Testermoon01 You are welcome, did this solve your problem? – Ahmed Akhtar Feb 26 '16 at 09:54
  • Yes, although I gave me a litte problem.. Whenever I put a space for the last value, I need to enter some key when the output is shown. – CJ Canlas Feb 27 '16 at 02:35
  • Glad to help! Welcome to SO. If this, or any other answer, helped you solve your problem, please [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) and/or [upvote](https://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow) it, so that later viewers may be able to track the solution easily. Also, please illustrate the little problem that you have mentioned, with an example, so that I may try to solve that too. – Ahmed Akhtar Feb 27 '16 at 07:02