Sentinel Loop Trouble

Hello. I am having issues with setting up a sentinel loop for my calculator. Whenever I put in the input to stop the loop, the loop continues as if nothing was wrong with the input.

I wasn't sure on how to properly create a sentinel loop and used examples found in the book or online. However, my calculator is using the operator variable to act as a sentinel value.

So whenever (opSign) is not a %, the loop should run normally.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	//Initial Value of opSign
	opSign = '+';
	
	while (opSign != '%', count <=3)
	{
		cout << "Please enter your operation -->" << endl;
		cout << endl;
		cin >> num1 >> slash >> den1 >> opSign >> num2 >> slash >> den2;
		cout << "Please enter your result -->" << endl;
		cout << endl;
		cin >> answerUser1 >> slash >> answerUser2;
		// Function Calls
		fractionCalc (num1, num2, num3, den1, den2, den3, opSign);
		fractionComparison (num1, num2, den1, den2, num3, den3, answerUser1, answerUser2, opSign, floatPoint, correct);
		count++;


	}


Thank you for your time!
I think what you're trying to do is:
while( opSign != '%' && count <= 3 )

you cannot have more than one logical expression in a while() loop.
Hello! Thank you for your reply!

I've converted the code to look like this but still I am facing problems.

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
	//Initial Value of opSign
	opSign = '+';
	
	while (opSign != '%')
	{
		while (count <=3)
		{
		cout << "Please enter your operation -->" << endl;
		cout << endl;
		cin >> num1 >> slash >> den1 >> opSign >> num2 >> slash >> den2;
			if (opSign == '%')
			{
				cout << "You've chosen to end the program" << endl;
			}
			else
			{
			cout << "Please enter your result -->" << endl;
			cout << endl;
			cin >> answerUser1 >> slash >> answerUser2;
			// Function Calls
			fractionCalc (num1, num2, num3, den1, den2, den3, opSign);
			fractionComparison (num1, num2, den1, den2, num3, den3, answerUser1, answerUser2, opSign, floatPoint, correct);
			count++;
			}
		}
	}


Essentially, I need to have the variable opSign end the loop but I also need the user to input opSign. The logic behind this is really confusing me. I really appreciate it if you could help me.


My output with this code shows as follows:

Please enter your operation -->

1/2 % 1/2
You've chosen to end the program
Please enter your operation -->

Last edited on
if you combine your two loops to be like my example above, then the loop will end if either opSign = '%' or count > 3;

also, you can force a loop exit after line 13 like this:
1
2
3
4
5
			if (opSign == '%')
			{
				cout << "You've chosen to end the program" << endl;
				break;
			}


that will only break out of the current loop (the "count <= 3" loop).
Last edited on
Thank you so much for your advice! I used your break; statement and the whole sentinel loop worked wonderfully!

Please have a wonderful day or night!
Last edited on
Topic archived. No new replies allowed.