Sentinel Value added into math of loop - WHY?

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.

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
#include  <iostream>
using namespace 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!
Last edited on
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!
also remove the system pause please.
also remove the system pause please.


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.
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
#include  <iostream>
using namespace std;

int main ()
{
    //declare variables
    double payroll = 0.0 , totalPayroll = 0.0;
    int storeNumber = 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;
                 if(payroll == -1) break; 
                 totalPayroll += payroll;

           }     //end while
           ++storeNumber;
    }
    cout << "Total Payroll: $" << totalPayroll;
    
    cin.ignore(); 
    return 0;
} 

that is what your working code will look like...
Last edited on
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!
if you noticed i put cin.ignore() before return 0
that will keep your consul open if that is why you put system pause.
Actually my console still closed. I had to put the system("pause") back in to keep it from closing. Thanks!
getch(); works also
Though getch() is non-standard.

Usually this should work:
1
2
std::cin.sync();
std::cin.get(); // cin.ignore() works too 
Topic archived. No new replies allowed.