帮助同时控制结构程序

Help!? While Control Structure Program!

本文关键字:程序 控制结构 帮助      更新时间:2023-10-16

我正试图在第二个循环中输入账号。如果我在"While"控制结构中输入"Enter the account number"。它将在第一个循环中打印两次。那么我该如何解决这个问题呢?

/* 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;
} 

我会这样构建代码:

while(true)
{
  cout << "Enter account number: ";
  cin >> aNum;
  if(aNum==-1) break;
  // ... Rest of your while loop ...
}

尝试使用下面的代码

/* 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;
} 

希望它能帮助。。。