Cannot stop sentinel loop from running away.

So, I have written one program already that is supposed to allow a user to input the word length of a story and the Authors skill level ( Ranging A, B, C). That program works like it's supposed to. The second part is to modify the program to allow a user to process multiple stories and authors by using a Prime read and Sentinel loop. When the value of -1 is entered it should stop and output the data.
My Problem is, I cannot get the sentinel loop to run properly. First, as soon as you enter wordLength it takes off and second, it seems to be skipping a lot of code. Meaning, when I insert a breakpoint and try to follow the program line by line, It will skip most of the code. I imagine my loop has something to do with it.
If more info is needed, just say the word. I can post the original program as well before modification.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

//authorPay variables
float skillLvl, a, b, c, A, B, C;
float wordLength, payRate, payOut;

//authorPay2 Variables 
int payments, totalPayments, paymentCounter;
float averagePayment;
int aClass, bClass, cClass;  
int sentinel = 0;
// Function Prototypes 
float PayOutTotal(float, float, float, float);
float payRateTotal(float, float);


int main() {
	
	wordLength = 0;
	payments = 0;

	cout << " Welcome To Author Pay! " << endl;
	while (wordLength != sentinel)  {
		totalPayments += payments;
		paymentCounter++;

		cout << "Please Enter The Word Length " << endl;
		cin >> wordLength;
		cout << endl;

		cout << "Please Enter The Authors Skill Level (-1 To Quit) " << endl;
		cin >> skillLvl;
		cout << endl;

		payRate = payRateTotal(wordLength, payRate);

		payOut = PayOutTotal(payRate, wordLength, skillLvl, payOut);
		
		
		
		

	}
	cout << "Length of Story (Words) " << setw(8) << ' ' << "Amount To Be Paid " << endl;
	cout << setw(12) << wordLength << setw(25) << ' ' << "$" << payOut << endl;

	cout << " The Total Number of Payments Caluculated: " << totalPayments << endl;
	cout << " The Average Amount Paid for a Story " << totalPayments / paymentCounter << endl;
	cout << " The Total Number of Payments Caluculated: " << totalPayments << endl;
	cout << " The Average Amount Paid for a Story " << totalPayments / paymentCounter << endl;


	
	return 0;

}
//Function Definitions 
float payRateTotal(float wordLength, float payRate)
{
	payRate = 0;
	if (wordLength < 7500) {
		payRate = 0.08 * wordLength;
	}
	else if (wordLength > 7500 && wordLength < 8000) {
		payRate = 600;
	}
	else if (wordLength > 8000 && wordLength < 17500) {
		payRate = 0.075 * wordLength;
	}
	else if (wordLength > 17500 && wordLength < 19000) {
		payRate = 1313;
	}
	else if (wordLength >= 19000) {
		payRate = 0.07 * wordLength;
	}
	return payRate;
}

float PayOutTotal(float payRate, float wordLength, float skillLvl, float payOut) {
	payOut = 0;
	if (skillLvl == 'A' || skillLvl == 'a') {
		payOut = payRate * 1.75;
		
	}
	else if (skillLvl == 'B' || skillLvl == 'b') {
		payOut = payRate * 1.25;
	}
	else if (skillLvl == 'C' || skillLvl == 'c') {
		payOut = payRate; 
	}

	return payOut;
}

Hello Mjimmie1,

First I would move all the global variables to the beginning of main where they should be. Try to always avoid using global variables that are not "const" or 'constexpr" qualified.

Since all your function calls pass the variables needed and all your functions receive something there is no need for global variables. The other side of this is if you use global variables there is no need to pass and receive the parameters because all the functions in the file can see these variables and that is what makes global variables such a problem because anything can change them.

Line 26 the while condition. In the end the condition is looking for a true or false ( 1 or 0) answer. So if "wordLength = 0 and sentinel = 0" and a comparison of "!=" what would your answer be?

No if you could actually enter the while loop the next problem is that you process 6 lines of code before you can enter a -1 to quit. Then you process three more lines just to get back to the while condition which is checking the wrong variables. They should be "skillLvl and sentinel"

I would rewrite the code as:
1
2
3
4
5
6
7
8
9
while (true)
{
    cout << "Please Enter The Authors Skill Level (A, B, C, D, -1 To Quit) ";  // <--- Removed the endl.
    cin >> skillLvl;

    if (skillLvl == -1)
        break;  // <--- Leave while loop.

    //  Your other lines of code. 


I was going to say that "sentinel" should be defined as: constexpr int sentinel{};, but doing the while loop differently tends to eliminate the need for this variable. So now it is just FYI.

The other thing I would is change all the "float"s to "double"s. "double" have a greater precision than a "float" and will store the number better and "double"s are preferred more often over "float"s.

A quick look at the function "PayOutTotal" I noticed the numbers "1.75" and "1.25" I would make these const global variables like: constexpr double PAYRATEA{ 1.75 };. As a global variable at the top of the program it is easy to find when a change is needed and it is best to stay away from magic numbers.

You might consider the same concept with the "payRateTotal" function.

I have not tested the program yet, so this is what I can see for now.

Hope that helps,

Andy
Thank You so much for your wisdom. I have not had time to revise the code yet, but you have helped me greatly. And thank you again for the additional info. I will most certainly make these changes.
Topic archived. No new replies allowed.