簡體   English   中英

不能弄清楚如何循環計算單個學生的總數

[英]Cant figure out how to calculate individual students totals in a loop

我對此程序有疑問。 我需要它來詢問用戶ID,然后詢問書籍代碼,然后詢問書籍成本。 個人可以輸入未知數量的書。 該程序需要計算出各個學生的書籍總數,然后問另一個做同樣的學生。 然后,程序必須顯示總計和書籍總數。 我似乎無法弄清楚該使用什么來跟蹤每個學生的條目。 通過閱讀有關數組的信息,我可以做到這一點。 但是我們還沒有到這一點。 教授希望我們循環執行此操作。 我很迷茫,任何幫助都會很棒。

#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{
    //Declare Variables.
    int student_id;
    char book_code;
    float book_cost;
    float tax_amount;
    float book_subtotal;
    const int SENTINEL = -9999;
    const double TAX = .07;
    float total_book_cost;
    int number_books;
    int total_books_sold;
    double grand_total;

    //Set Variables to Zero.
    number_books = 0;
    total_book_cost = 0.00;
    grand_total = 0.00;

    //Set Decimal to two places.
    cout << fixed << showpoint;
    cout << setprecision(2);

    //Input Data
    cout<<"Please enter your Student ID, then press enter."<<endl;
    cin>>student_id;
    while (student_id != SENTINEL){
        cout<<"Please enter your Book Code, then press enter."<<endl;
        cin>>book_code;
        cout<<"Please enter the cost of the book, then press enter."<<endl;
        cout<<"$"; cin>>book_cost;
        tax_amount = book_cost * TAX;
        book_subtotal = book_cost + tax_amount;
        total_book_cost += book_subtotal;
        number_books++;
        cout<<"\tStudent Textbook Purchases Report"<<endl;
        cout<<"********************************************"<<endl;
        cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
        cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
        cout<<"--------------------------------------------"<<endl;
        cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
        setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
        cout<<endl;
        cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
        cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
        cout<<"Please enter your Student ID, then press enter."<<endl;
        cin>>student_id;
    }
    grand_total += total_book_cost;
    total_books_sold += number_books;
    cout<<"**************************************************"<<endl;
    cout<<"Grand Totals:"<<endl;
    cout<<"Total number of students who purchased books:"<<endl;
    cout<<"Total number of books sold:"<<endl;
    cout<<"Total cost of all books and taxes:"<<setw(9)<<"$"<<grand_total<<endl;

    //Can put grand totals here

    system("Pause");
    return 0;
}

您可以這樣使用循環:

#include <iostream>
#include <iomanip>

using namespace std;
int main ()
{

    //Set Decimal to two places.
    cout << fixed << showpoint;
    cout << setprecision(2);


    int total_books_sold = 0;
    double grand_total = 0.0;

    const int SENTINEL = -9999;
    int     student_id = SENTINEL;
    //Input Data
    cout<<"Please enter your Student ID, then press enter."<<endl;
    cin>>student_id;

    while (student_id != SENTINEL){

        double  total_book_cost = 0.0;
        int     number_books = 0;
        char    book_code = '\0';
        while (true) 
        {
            cout<<"Please enter your Book Code, then press enter."<<endl;
            cin>>book_code;

            if (book_code == 'x')
                break;

            float   book_cost;
            cout<<"Please enter the cost of the book, then press enter."<<endl;
            cout<<"$"; cin>>book_cost;

            const double TAX = .07;
            double tax_amount = book_cost * TAX;
            double book_subtotal = book_cost + tax_amount;

            total_book_cost += book_subtotal; 
            number_books++;

            cout<<"\tStudent Textbook Purchases Report"<<endl;
            cout<<"********************************************"<<endl;
            cout<<"Student"<<"\tBook"<<"\tBook"<<"\tTax"<<"\tBook"<<endl;
            cout<<"Id"<<"\tCode"<<"\tCost"<<"\tAmount"<<"\tSubtotal"<<endl;
            cout<<"--------------------------------------------"<<endl;
            cout<<student_id<<setw(5)<<book_code<<setw(8)<<"$"<<book_cost<<
                setw(3)<<"$"<<tax_amount<<setw(4)<<"$"<<book_subtotal<<endl;
            cout<<endl;

        };


        grand_total += total_book_cost;
        total_books_sold += number_books;

        cout<<"Total number of books purchased:"<<setw(8)<<number_books<<endl;
        cout<<"Total books cost including tax:"<<setw(9)<<"$"<<total_book_cost<<endl;
        cout<<"Please enter your Student ID, then press enter."<<endl;
        cin>>student_id;
    }


    cout<<"**************************************************"<<endl;
    cout<<"Grand Totals:"<<endl;
    cout<<"Total number of students who purchased books:"<<endl;
    cout<<"Total number of books sold:"<<endl;
    cout<<"Total cost of all books and taxes:"<<setw(9)<<"$"<<grand_total<<endl;

    //Can put grand totals here

    system("Pause");
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM