0

2回目のループで「口座番号を入力」させようとしています。「口座番号を入力してください」を「While」制御構造に入れると。最初のループで 2 回印刷されます。では、どうすればこれを修正できますか?

/* Make a program that will determine if a department store customer has exceeded the
credit limit on a charge account*/
#include <iostream>

#include <iomanip>

using namespace std;

int main()
{
    int aNum,Always;

    double balance,iTotal,cTotal,cLimit,NewBal;

    cout << "Enter account number: ";
        cin >> aNum;

     while ( aNum != -1 )
    {

        cout << "Enter beginning balance: ";
            cin >> balance;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter total charges: ";
            cin >> iTotal;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter total credits: ";
            cin >> cTotal;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter credit limit: ";
            cin >> cLimit;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision(2);

        cout << endl;

        NewBal = balance + iTotal - cTotal;

       if ( NewBal >= cLimit ) {

                    cout << "Account: " << setw(9) << aNum << endl;
                    cout << "Credit limit: " << cTotal << endl;
                    cout << "Balance: " << setw(9) << balance << endl;
                    cout << "Credit limit exceeded." << endl;
                    cout << endl;
       }
    }
    return 0;
} 
4

2 に答える 2

1

コードを次のように構成します。

while(true)
{
  cout << "Enter account number: ";
  cin >> aNum;
  if(aNum==-1) break;

  // ... Rest of your while loop ...
}
于 2012-08-01T05:43:28.500 に答える
0

以下のコードを使用してみてください

/* Make a program that will determine if a department store customer has exceeded the
credit limit on a charge account*/
#include <iostream>

#include <iomanip>

using namespace std;

int main()
{
    int aNum,Always;

    double balance,iTotal,cTotal,cLimit,NewBal;

    do 
    {
        cout << "Enter account number: ";
        cin >> aNum;
        if(aNum != -1)
        {
        cout << "Enter beginning balance: ";
            cin >> balance;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter total charges: ";
            cin >> iTotal;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter total credits: ";
            cin >> cTotal;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision( 2 );

        cout << "Enter credit limit: ";
            cin >> cLimit;
            cout << setiosflags( ios::fixed | ios::showpoint ) << setprecision(2);

        cout << endl;

        NewBal = balance + iTotal - cTotal;

       if ( NewBal >= cLimit ) {

                    cout << "Account: " << setw(9) << aNum << endl;
                    cout << "Credit limit: " << cTotal << endl;
                    cout << "Balance: " << setw(9) << balance << endl;
                    cout << "Credit limit exceeded." << endl;
                    cout << endl;
       }
     }
    }while ( aNum != -1 );
    return 0;
} 

それが役に立てば幸い...

于 2012-08-01T05:43:57.290 に答える