输入摘要信息-999后无法使程序退出

Cant make the programm exit after -999 input with summary information

任务: 编写一个(理论上)保持打开状态的程序,并允许用户全天输入数字。 用户可以输入 0 到 50 之间的数字。超出该范围的数字(-999 除外)无效,用户必须重新输入有效数字。 当用户输入数字时,它会添加到总和中,并且使用该程序的用户数量会增加。 当值-999。输入-999时,打印学生总数、教材总数、平均教材数。

    #include <iostream>
    using namespace std;

int main () {

    const int minBooks = 0, maxBooks = 50;
    int books;
    int sum=0;
    int student = 1;

do {
    cout << "Books: " << endl;
    cin >> books;
    
    student++;
    sum += books;
   
   if (books == -999) {
    cout << "Books: " << sum << "\n" << "Students: " << student << "\n" << "Average" << sum/student<< endl;
    break;
    } 
    
    while (books < minBooks || books > maxBooks)
    {
        cout << "You should at least have " << minBooks << " but no more than " << maxBooks << endl;
        cout << "How many books did you purchased?"<< endl;
        cin >> books;
    } 

} while (books!= -999);

}

我的问题是我无法在用户输入 -999 后退出程序。 我尝试改变位置,在循环中使用,在循环外使用 如果 -999 有效,则它不会验证输入


针对“if”进行了更改,现在可以使用了 但是,它不会总结书籍,只接受最后一个输入 -999


在两个地方换书求和后,现在我的平均数不正确,因为学生人数不对;我试着做减法,但后来我的平均值变成了负值

搞清楚了,谢谢大家的帮助! 以下是一些有用的更改:

#include <iostream>
using namespace std;

int main () {

const int minBooks = 0, maxBooks = 50;
int books;
int sum=0;
int student = 0;

do {
cout << "Books: " << endl;
cin >> books;
student++;
sum += books;

if (books == -999) {
student = student - 1;
sum += 999;
cout << "Here is your summary: " << endl;
cout << "Books: " << sum << "\n" << "Students: " << student << "\n" << 
"Average: " << sum/student<< endl;
break;
} 

while (books < minBooks || books > maxBooks)
{
    cout << "You should at least have " << minBooks << " but no more than " << maxBooks << endl;
    cout << "How many books did you purchased?"<< endl;
    cin >> books; 
} 

} while (books!= -999);

}