我最近开始学习C ++并且一直关注加速C ++这本书。我在第3章,我试图运行书中给出的程序。我正在使用我的MAC OS上的CLion。代码如下:
#include "iomanip"
#include "ios"
#include "iostream"
#include "string"
#include "vector"
#include "algorithm"
using std::cin; using std::cout;
using std::endl; using std::string;
using std::setprecision; using std::streamsize;
using std::vector; using std::sort;
int main()
{
// Ask for and read student's name
cout << "Please enter your name: ";
string name;
cin >> name;
cout << "Hello, "<< name << "!"<< endl;
// Ask for and read mid-term and final grades
cout << "Please enter your mid-term and final grades: ";
double midterm_grade, final_grade;
cin >>midterm_grade >> final_grade;
// Ask for homework grades
cout << "Enter all your homework grades, followed by end-of-file";
vector<double> homework;
double x;
//Invariant : Homework contains all homework grades read so far
while (cin >> x)
{
homework.push_back(x);
}
// Check that the student entered some homework grade
typedef vector<double>:: size_type vector_size;
vector_size size = homework.size();
if(size == 0)
{
cout << "You must enter your grades. Please try again" << endl;
return 1;
}
// Sort the grades
sort(homework.begin(), homework.end());
// Compute the median output grade
vector_size mid = size/2;
double median;
median = size % 2 == 0 ? (homework[mid] + homework[mid-1])/2 : homework[mid];
// Compute and write the final grade
streamsize prec = cout.precision();
cout << "Your final grade is :" << setprecision(3) << 0.2*midterm_grade + 0.4*final_grade + 0.4*median<< setprecision(prec) << endl;
return 0;
}//
该程序编译得很好,我可以输入名称和成绩。但是当我按下Ctrl + D(这是从终端确认的EOF,也在这里建议How to signal EOF of stdin when in mac osx terminal?)或Cmd + D(如同建议的那样)时。
当我按下Ctrl + D时,程序才会重新启动。在按下Cmd + D时,它有一些不寻常的行为。它有时可能会起作用,有时则不起作用。我也尝试查看首选项 - &gt;键盘映射,但我无法找到要设置的参数。
我想我错过了一些非常基本的关键设置。 谢谢您的帮助。
答案 0 :(得分:1)
显然有效的方法是在运行窗口中发送“EOF”,然后程序每次运行都没有任何问题。
答案 1 :(得分:0)
os x与clion存在已知问题。在查找 - >操作中,禁用 run.process.with.pty ,它应该如下所示:
您需要做的是在新的空行中按cmd + d。在您的情况下,输入作业成绩,在下一个新的空行中,按cmd + d。