簡體   English   中英

C ++使用cin.get()暫停該程序

[英]C++ pause this program using cin.get()

我在不同的配置中使用了cin.ignore() / cin.clear() / cin.sync() ,但是只能使用getch()停止我的程序。 有任何想法嗎?

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

輸入緩沖區中可能會有換行符,因此cin.get()將在輸入緩沖區中等待該換行符。 要暫停程序,您可以添加另一個cin.get()或使用

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

它將忽略所有字符,直到換行符為止。 還包括限制#include <limits>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM