命令提示符保持关闭,C ++和Windows

时间:2015-12-27 04:06:48

标签: c++ visual-studio command-prompt

我一直在阅读“Accelerated C ++”一书,并完成了以下练习:

    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    #include <vector>

    using std::cin;         using std::sort;
    using std::cout;        using std::streamsize;
    using std::endl;        using std::string;
    using std::setprecision;
    using std::vector;

    int main()
    {
        cout << "please enter your first name: ";
        string name;
        cin >> name;
        cout << "hello, " << name << "!" < endl;

        cout << "please enter your midterm and final exam grades: ";
        double midterm, final;
        cin >> midterm >> final;

        // read hw grades
        cout << "enter all hw grades followed by end of file: ";
        vector<double> homework;
        double x;

        while(cin >> x)
            homework.push_back(x);

        typedef vector<double>>::size_type vec_sz;
        vec_sz size = homework.size();

        if(size == 0){
            cout << endl << "re-enter grades" << endl;
            return 1;
        }
        //compute the median
        sort(homework.begin(),homework.end());
        vec_sz mid = size/2;
        double median;
        median = size % 2 == 0 ? (homework[mid]+homework[mid-1])/2 : homework[mid];

        streamsize prec = cout.precision();
        cout << "final grade is " << setprecision(3) << .2*midterm+.4*final+.4*median
             << setprecision(prec) << endl;

        cin.get();
        cin.get();

        return 0;
    }

程序完成执行后,命令窗口将关闭。我正在使用Visual Studio 2015,并尝试了各种修复,其中包括使用cin.get()以及cin.ignore()后跟cin.get()甚至cin.get()重复两次。唯一似乎有效的方法是system("pause")但我宁愿不使用它,因为它似乎是糟糕的编程习惯。我感到茫然,并希望得到任何帮助,因为我相信我将来会遇到这种情况。谢谢你们。

0 个答案:

没有答案