存储每次迭代的值然后总计它们的函数

时间:2018-03-03 00:55:14

标签: c++

所以我正在尝试编写一个程序,该程序将一组书的总成本给定一定值,然后循环回到开头,以获得一组新书的总数,直到用户告诉它为止停。在课程结束时,将显示所有书籍的总费用。我很难写这个,谢谢你对此有任何帮助,对于我仍在学习的凌乱代码感到抱歉。

此处列出的代码:

 #include <iostream>

using namespace std; 

const float kReqNew = 0.90;
const float kReqOld = 0.65;
const float kOptNew = 0.40;
const float kOptOld = 0.20;

class Book {

private: 
    float singleCost_;
    int NeworOld_;
    int ReqorOpt_;
    int TotalBooksNeeded_;
    int BookNumber_;
    int NumOnHand_;
    int prospectiveEnrole;
    double totalCost_;

public:
    void buildData();
    void determineBooks();
    void totalCosts();
    void layOut();
    void totalCostAdd();

};

void Book::buildData() {

    cout << "Please enter: " << endl;
    cout << "Book Number:";
    cin >> BookNumber_;
    cout << "Book Price: ";
    cin >> singleCost_;
    cout << "How many are in stock: ";
    cin >> NumOnHand_;
    cout << "Prospective Enrolment: ";
    cin >> prospectiveEnrole;
    cout << "Required(1), Optional(0)";
    cin >> ReqorOpt_;
    cout << "New(1), Old(0): ";
    cin >> NeworOld_;
}
void Book::determineBooks(){

    if(NeworOld_ == 1 && ReqorOpt_ == 1){

        TotalBooksNeeded_ = NumOnHand_ * kReqNew;
    }
    else if(NeworOld_ == 1 && ReqorOpt_ == 0){

        TotalBooksNeeded_ = NumOnHand_ * kOptNew;
    }
    else if(NeworOld_ == 0 && ReqorOpt_ == 1){

        TotalBooksNeeded_ = NumOnHand_ * kReqOld;
    }
    else if(NeworOld_ == 0 && ReqorOpt_ == 0){

        TotalBooksNeeded_ = NumOnHand_ * kOptOld;
    }
    cout << "Total Number of Books Needed: " << "$" << TotalBooksNeeded_ << endl;
}
void Book::totalCosts(){

    totalCost_ = TotalBooksNeeded_ * singleCost_;
    cout << "The total cost of the books is: " << totalCost_ << endl;

}

void Book::layOut(){

    cout << BookNumber_<<endl;
    cout << "$" << singleCost_ << endl;
    cout << NumOnHand_ << endl;

}

void Book::totalCostAdd(){

    double* newTotalCost;
    *newTotalCost += totalCost_;
    cout << newTotalCost << endl;
}   



int main() {
    Book books;
    int exitNum;
    cout << "\t\t\t ****************\n" << "\t\t\t  Book Calculator\n" << "\t\t\t ****************\n";
    do{
        cout << "*******************************************\n";
        books.buildData();
        cout << "*******************************************\n";
        books.layOut();
        cout << "*******************************************\n";
        books.determineBooks();
        books.totalCosts();
        cout << "*******************************************\n";
        cout << "Would you like to continue: Yes(1), No(0)" << endl;
        cin >> exitNum;
        cout << "*******************************************\n"; 
        books.totalCostAdd();

    } while(exitNum != 0); 
}

再次感谢您的帮助。

0 个答案:

没有答案