返回

C++在程序999输入后不能用概要信息退出程序

发布时间:2022-04-13 05:35:51 365
# golang# c++

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

    #include 
    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有效,那么它不会验证输入


changed while for "if" and now it works however, it doesn't sum up books and takes only last input which is -999


在把两个地方的书换成总和后,由于学生人数错误,现在我的平均数不正确;我试着去消瘦,但后来我的平均值变成了负数

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像