0

I have use cin.ignore() / cin.clear() / cin.sync() in different configuration, but can only stop my program using getch(). Any ideas?

#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;
}

1 Answers1

-1

There might be a newline character in the input buffer, so cin.get() takes that newline character waiting in the input buffer. To pause the program, you can add another cin.get() or use

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

which will ignore all characters till the newline. Also include limits #include <limits> .

XZ6H
  • 1,779
  • 19
  • 25