2012-08-01 57 views
0

我想在第二個循環中輸入「賬號」。如果我在「While」控制結構中輸入「輸入帳號」。它會在第一個循環中打印兩次。那我該如何解決這個問題?幫助!?雖然控制結構程序!

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

您好,歡迎來到程序員。儘管有這個名字,但我們並不是這個問題的網站。我們將看到有關遷移到堆棧溢出的情況。祝你有愉快的一天:) – 2012-08-01 02:54:38

+0

哦,對不起,我是新來的。 – 2012-08-01 02:57:34

+1

沒關係 - 我們會將問題標記爲自動遷移到Stack Overflow - 這是此類問題更合適的站點。 – Deco 2012-08-01 03:05:20

回答

1

我結構代碼:

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

    // ... Rest of your while loop ... 
} 
+0

哇它的工作:D! Ty一件事,你爲什麼接下來加上(True)? – MintIceCream 2012-08-02 01:32:27

+0

@MintIceCream while(true)表示「永遠循環」。 'break'表示「停止循環」。 – 2012-08-02 02:06:40

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

代碼希望它可以幫助...