I am writing a program that should allow the user to enter however many payroll amounts they want for 3 different stores. It should then display the total payroll for the three stores. The first time through the loop is fine but the second time through, even though it appears that the loop conditions still have not been met, it immediately goes to the end of the program. Any suggestions? (Please keep in mind I've been coding for less than a month)

#include <iostream>
using namespace std;

int main()
{
	int numStore= 1;
	int payrollAmount= 0;
	int totalPayroll= 0;

	do
	{
		totalPayroll+= payrollAmount;

		cout << "Store " << numStore << ":" << endl;

		while (payrollAmount >= 0)
		{
		cout << "Enter Payroll Amount (Negative Number to Stop): $";
		cin >> payrollAmount;
		} 
	numStore +=1;
	} while (numStore < 4);

	cout << "Total Payroll: $" << totalPayroll << endl;

	return 0;
}

Shouldn't you do your totalPayroll += payrollAmount in the inner loop? As it is, your inner loop just prompts for and accepts a value for payrollAmount.

Edited 5 Years Ago by gerard4143: n/a

That fixes the total Payroll problem but doesn't explain why it won't repeat the loop. Still stumped on that one.

Look at your inner condition

while (payrollAmount >= 0)

To exit this loop you must set payrollAmount < 0. What happens the next time around? payrollAmount is still set to < 0. You have to set payrollAmount back to zero when you exit the inner loop.

Edited 5 Years Ago by gerard4143: n/a

It is repeating the loop. Try a couple of cout lines to find out where you're going wrong(ie right before line 21).

Yep thats it! I knew it was something simple, but I'm a noob so even the simplest fo things give me trouble sometimes. Its working as it should now. Thanks for the help!

Yep thats it! I knew it was something simple, but I'm a noob so even the simplest fo things give me trouble sometimes. Its working as it should now. Thanks for the help!

*of

This question has already been answered. Start a new discussion instead.