While structure program! Help!

I'm trying to make "enter the account number" on the second loop. If I put "Enter the account number" in the "While" control structure. It would print twice in the first loop. So how can I fix this? *NOTE* I mean after you build and run it!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* 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; 
}  
Last edited on
I only see 1 while loop there ?

I'm not sure what you want. There is an option where you can set a bool flag to determine if that piece of code is to be executed in the while loop. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool baccountno = false;
 while ( aNum != -1 ) 
 { 
 
   if(baccountno == false)
   {
       cout << "Enter account number: "; 
       cin >> aNum; 
   }

   ....
   if(changeaccount)
   {
      baccountno = true;
   }
Sorry let me clarify that. I mean like after you build and run it, it will do the loops. For example, loop 1,loop 2,loop3, and so on. I set it as undefinite loop?
But what are you trying to achieve?
Your program is running fine after compiling it in gcc.
I think i gout the problem.
add this just before while loop:
 
aNum=0;


and move this inside the while loop (at the begining):

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


so it would look like this:

1
2
3
4
5
6
...
aNum=0;
while(aNum!=-1){
 cout << "Enter account number: "; 
        cin >> aNum; 
...
Last edited on
Topic archived. No new replies allowed.