So I built a program to take an indefinite number of stores and then input an indefinite number of payrolls for each store and add them up to get a total payroll for all stores.
#include <iostream>
usingnamespace std;
int main ()
{
//declare variables
double payroll = 0.0;
int storeNumber = 0;
double totalPayroll = 0.0;
while (storeNumber < 4) //begin loop
{
cout << "Enter Store Number: ";
cin >> storeNumber;
while (payroll != -1)
{
cout << "Enter Payroll Amount: $" << "(Enter -1 to stop)";
cin >> payroll;
totalPayroll = totalPayroll + payroll;
} //end while
storeNumber = storeNumber + 1;
}
cout << "Total Payroll: $" << totalPayroll;
system("pause");
return 0;
} //end of main function#include <iostream>
When I run the program my sentinel value of -1 is being calculated into the payroll math. Essentially if store 1 has payroll amounts of $50 and $50, the total payroll is returned as $99. I've searched the forums and then several google searches all with no help. I tried changing the while (payroll != '-1') but that just errors out. My textbook is absolutely no help when it comes to troubleshooting errors like this.
There also a problem with the store loop that I have yet to iron out. But one thing a time. Thanks for the help!
It's because you read in the value, work it into the math, then see if it is -1 and decide whether to leave the loop. You need to change the order you do that.
Should I change it to a post control loop then? Unfortunately I'm at work right now and can't test it until tomorrow afternoon. I think I understand what you're saying, I'm just not sure how to correct it. Thanks for pointing in the right though!
Seriously that is all you have to offer? The system pause is not causing my problem here. And considering that my instructor specifically told us to leave that in place, perhaps I should leave it. No offense but stop trolling for the little things and try to be of genuine assistance.
ok if you want help put this code into your inner while loop...
if(payroll == -1) break;
im offering help if you do not want it than say so. you do not have to call me a troll... i was saying that system commands are big holes in your program and are OS specific.
Thank you very much! I'm sorry about the troll thing; it just trips my trigger when someone jumps into a tread and offers info of no value to the original discussion. As for the system pause thing, I read the thread in this forum about it being a serious security issue and will figure how to get rid of eventually. Thanks!