2012-08-01 56 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; 
} 

代码希望它可以帮助...