1
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

//Named constants residential customers
const double RES_BILL_PROC_FEES = 4.50;
const double RES_BASIC_SERV_COST = 20.50;
const double RES_COST_PREM_CHANNEL = 7.50;

//Named constants business customers
const double BUS_BILL_PROC_FEES = 15.00;
const double BUS_BASIC_SERV_COST = 75.00;
const double BUS_BASIC_CONN_COST = 5.00;
const double BUS_COST_PREM_CHANNEL = 50.00;

int main()
{
   //Variable declarations
    int accountNumber;
    char customerType;
    int numOfPremChannels;
    int numOfBasicServConn;
    double amountDue;

    //declaring file streams
    ifstream inFile;
    ofstream outFile;

    //open the file streams
    inFile.open("input.txt");
    outFile.open("output.txt");

    //check wheather file exists or not
    if (inFile.fail())
    {
        cout << "The input file cannot be found! ";
        return 1;
    }

    //output console message
    cout << "\n==========================================\n\n";
    cout << "     PROGRAM TO COMPUTE THE CABLE BILL\n";
    cout << "\n==========================================\n\n";
    //output outfile message
    outFile << "\n==========================================\n";
    outFile << "              Customer Details";
    outFile << "\n==========================================\n";

    //loop until end of file
    while (!inFile.eof())
    {
    cout << "The account number is: ";
    inFile >> accountNumber;
    cout << accountNumber<<"\n";

    cout << "The customer type: "
         << "R or r (Residential), "
         << "B or b (Business):  ";
    inFile >> customerType;
    cout << customerType << endl;

    //switch to residential or business customer type
    switch (customerType)
        {
        case 'r':
        case 'R':
            cout << "Enter the number"
                 << " of premium channels: ";
            cin >> numOfPremChannels;
            cout << "\n\n";

            amountDue = RES_BILL_PROC_FEES
                       + RES_BASIC_SERV_COST
                       + numOfPremChannels *
                         RES_COST_PREM_CHANNEL;
            //write to output file
            outFile << setw(25) << left << "\nCustomer Account Number: "
                    << accountNumber << endl;
            outFile << setw(25) << left << "Amount Due: "
                    << fixed << setprecision(2) << "$" << amountDue << endl;

            break;

        case 'b':
        case 'B':
            cout << "Enter the number of basic "
                 << "service connections: ";
            cin >> numOfBasicServConn;


            cout << "Enter the number"
                 << " of premium channels: ";
            cin >> numOfPremChannels;
            cout << "\n\n";

            if (numOfBasicServConn <= 10)
                amountDue = BUS_BILL_PROC_FEES
                            + BUS_BASIC_SERV_COST
                            + numOfPremChannels *
                              BUS_COST_PREM_CHANNEL;
            else
                amountDue = BUS_BILL_PROC_FEES
                            + BUS_BASIC_SERV_COST
                            + (numOfBasicServConn - 10) *
                               BUS_BASIC_CONN_COST
                            + numOfPremChannels *
                              BUS_COST_PREM_CHANNEL;

            //write to output file
            outFile << setw(25) << left << "\nCustomer Account Number: "
                    << accountNumber << endl;
            outFile << setw(25) << left << "Amount Due: "
                    << fixed << setprecision(2) << "$" << amountDue << endl;

            break;

        default:
            //write to output file
            outFile << "\nThe account number " << accountNumber << " has an invalid customer type." << endl;

            break;
        } //end switch

    } //end eof while loop

    //close input file
    inFile.close();

    //close output file
    outFile.close();

    return 0;
}

最後のケースが常に2回実行/印刷される理由を誰かに説明できますか? これは、授業のために書かなければならない基本的なプログラムですが、なぜ機能しないのかわかりません。.eof() while ループに問題がありますか? do-while ループを使用してみましたが、同じ結果が得られます。また、デフォルトのケースを含めずに入力ファイルを変更しようとしましたが、それでも最後のセットを 2 回実行しています。

**基本的な接続とチャネルの数については、有効な整数を入力してください。これは私の入力ファイルです:

157744232 b
253453534 r
335345435 R
445345345 B
545345345 t
4

2 に答える 2

4

これはswitch構造のためではなく、eof呼び出しのためです。関数はtrue、次のアイテムの読み取りに失敗した後にのみ戻り始めます。

ループを無限ループに変更し(for (;;)またはwhile(true)、関係ありません)eof、データを読み込んだ後にチェックを実行します。を取得した場合はtrue、 を使用breakしてループを終了します。switchそれ以外の場合は、ステートメントに進みます。

ループの構造は次のようになります。

for (;;) {
    cout << "The account number is: ";
    if (!(inFile >> accountNumber))
        break;
    cout << accountNumber<<"\n";
    cout << "The customer type: "
         << "R or r (Residential), "
         << "B or b (Business):  ";
    if (!(inFile >> customerType))
        break;
    cout << customerType << endl;
    switch (customerType) {
        ... // The rest of your code
    }
}
于 2013-09-25T19:50:37.983 に答える