1

So I want to get a list of numbers. So 1,2,3-5 would say 1,2,3,4,5 This is what I have coded thus far

cin>>num; 
vec.push_back(num);
if(cin.peek() == ',')
    cin.ignore();

How can I do the range part?

Farhad
  • 4,119
  • 8
  • 43
  • 66
Alex
  • 23
  • 2

1 Answers1

1

Click Here to read more about cin.peek()

Click Here for a great Stack Overflow question

Click Here I used this source to help you out.

I am assuming you are using namespace std If you need more clarification comment and I will add more comments to my code.

 cin>>ws;       //eats up white spaces
 cout.flush(); 
    do          //loop to check every number
    {
        cin>>num1; 
        num_vec.push_back(num1);
        if(cin.peek() == ',')
        {
            cin.ignore();
        }
        else if(cin.peek() == '-')      
        {
            cin.ignore();
        //if it sees a dash it will ignore the dash
       // similar to what you did with your comma 
            cin>>num2;
            for(++num1; num1<=num2; num1++)
            {
                num_vec.push_back(num1);
      //keeps adding 1 to that range and pushing it back to vector 
            } 
                if(cin.peek() == ',')
                {
                    cin.ignore();
                }       
        }
    }while(cin.peek() != '\n');
O2Addict
  • 147
  • 11