2017-04-23 96 views
0

我使用cin.ignore()/cin.clear()/cin.sync()以不同的配置,但只能使用getch()停止我的程序。有任何想法吗?C++使用cin.get暂停程序()

#include <iostream> 
#include <sstream> 
#include <vector> 
#include <conio.h> 


using namespace std; 

int main() { 
    string line; 
    if (!getline(cin, line)) 
     return 0; 
    istringstream stream(line); 
    string name; 
    stream >> name; 
    cout << name; 
    stream >> name; 
    cout << ' ' << name; 
    double points; 
    double sum = 0; 
    vector <double> sums; 
    while (stream >> points) { 
     sum += points; 
     sums.push_back(points); 
    } 
    cout << ' ' << sum << endl; 
    int persons; 
    for (persons = 1; getline(cin, line); ++persons) { 
     stream.clear(); 
     stream.str(line); 
     stream >> name; 
     cout << name; 
     stream >> name; 
     cout << ' ' << name; 
     sum = 0; 
     for (int problem = 0; problem<sums.size(); ++problem) { 
      stream >> points; 
      sum += points; 
      sums[problem] += points; 
     } 
     cout << ' ' << sum << endl; 
    } 
    cout << endl; 
    for (int problem = 0; problem<sums.size(); ++problem) 
     cout << problem + 1 << ' ' << sums[problem]/persons << endl; 
    _getch(); 
    return 0; 
} 
+0

[请不要这样做](http://stackoverflow.com/a/36374595/560648) –

回答

-1

有可能是在输入缓冲区换行符,所以cin.get()需要的是换行字符的输入缓冲区中等待。要暂停程序,你可以添加其他cin.get()或使用

cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

这会忽略所有的字符,直到换行。还包括限制#include <limits>

+0

我已经尝试过,但可惜它不起作用。我正在使用VS2017和Command Arguments: Settius