Stop a certain part of loop from repeating

What the Output SHOULD be: The bold part should only be in the first account number, but my output is repeating it for all the other account numbers as well.

Enter account number (-1 to end):100
Enter beginning balance: 5394.78
Enter total charges: 1000.00
Enter total credits: 500.00
Enter credit limit: 5500.00
Account: 100
Credit limit: 5500.00
Balance: 5894.78
Credit Limit Exceeded.


Enter account number (-1 to end): 200
Enter beginning balance: 1000.00
Enter total charges: 123.45
Enter total credits: 321.00
Enter credit limit: 1500.00

Enter account number (-0 to end): 300
Enter beginning balance: 500.00
Enter total charges: 274.73
Enter total credit limit: 800.00



My Code:
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
#include <iostream>
using namespace std;

int main()
{
	int accountNumber = 0;
	float beginningBalance = 0;
	float totalCharges = 0;
	float totalCredits = 0;
	float newBalance = 0;
	float creditLimit = 0;

	// prompt the user for input (-1 is the sentinel value)
	cout << "Enter account number or (-1 to quit): ";
	cin >> accountNumber;
	
	while(accountNumber != -1)	
	{
		
		cout << "Enter beginning balance: ";
		cin >> beginningBalance;

		cout << "Enter total charges: ";
		cin >> totalCharges;

		cout << "Enter total credits: ";
		cin >> totalCredits;

		cout << "Enter credit limit: ";
		cin >> creditLimit;
	
		newBalance = beginningBalance + totalCharges - totalCredits; // calculation for the new balance
		
		cout << "New balance is: " << newBalance << endl; 
		cout << "Account: " << accountNumber << endl;
		cout << "Credit limit: " << creditLimit << endl;
		cout << "Balance: " << beginningBalance << endl;
		
			if (newBalance > beginningBalance)
				cout << "Credit Limit Exceeded." << endl;
			else
				cout << "Credit Limit Not Exceeded. " << endl;
	
		cout << "\nEnter the account number or (-1 to quit): "; 
		cin >> accountNumber;
	}
}



My code's output:

Problems:
1) The italics should not be there. It should only be there for the first paragraph.
2) My Credit limit has no decimals, however it's a float. I'm not sure.

Enter account number or (-1 to quit): 100
Enter beginning balance: 5394.78
Enter total charges: 1000.00
Enter total credits: 500.00
Enter credit limit: 5500.00
New balance is: 5894.78
Account: 100
Credit limit: 5500
Balance: 5394.78
Credit Limit Exceeded.

Enter the account number or (-1 to quit): 200
Enter beginning balance: 1000.00
Enter total charges: 123.45
Enter total credits: 321.00
Enter credit limit: 1500.00
New balance is: 802.45
Account: 200
Credit limit: 1500
Balance: 1000
Credit Limit Not Exceeded.


Enter the account number or (-1 to quit): 300
Enter beginning balance: 500.00
Enter total charges: 274.73
Enter total credits: 100.00
Enter credit limit: 800
New balance is: 674.73
Account: 300
Credit limit: 800
Balance: 500
Credit Limit Exceeded.


Enter the account number or (-1 to quit):
One thing you could do is to add a variable in your loop which you can initialize to say, 0.
When you reach the part of the code you want to be repeated only once use an if statement, and increase the counter at the end of the loop.
For example

1
2
3
4
5
6
7
8
9
10
11
12
while(accountNumber != -1)
{
   int i = 0;
   // Enter code here
   if(i=0)
   {
        //Enter the part of the code you want to repeat only the first time here
    }
    //Enter the rest of the code here
    i++;
}


This way the code in the if statement will only be executed when i=0, which only happens the first time, since you increment i at the end of the loop.
You can use the continue statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char** argv) {
    bool flag = false;
    int acc_no = 10;
    while (acc_no!=-1) {
        acc_no --;
        cout <<"This gets printed always:" << acc_no << endl;
        if(!flag)
            flag = true;
        else
            continue;
        cout << "This gets printed only for the first time" << endl;
    }
    return (EXIT_SUCCESS);
}
Thank you kindly.
Topic archived. No new replies allowed.