如何使用cin或getline()一次处理多个输入,而只按“Enter”一次?

时间:2010-10-08 21:38:55

标签: c++ user-input

nodeType* buildSet()
{
   nodeType *first, *newNode, *last;
   first = NULL;

   int num = 0;
   string input = "";

   getline(cin,input);
   stringstream myStream(input);

   while(myStream >> num)
//   while(num != -999)   
   {      
      newNode = new nodeType;
      newNode->info = num;
      newNode->link = NULL;

      if(first == NULL)
      {
         first = newNode;
         last = newNode;
      }
      else
      {
         last->link = newNode;
         last = newNode;
      }
         getline(cin,input);      
//       cin >> num;  
   }

   return first;
}

1 个答案:

答案 0 :(得分:0)

我通过删除第二个“getline(cin,input)”来解决问题。