Flag Control while loop

May 5, 2020 at 1:18pm
hello there, im taking c++ course, and the professor asked us for an assignment but im not sure im following the question which is

"Write a complete C++ program that read some integer marks and average them, the program will accept numbers until you enter zero, note that average will be for all marks between 40 and 100 only."

can someone explain or help?
May 5, 2020 at 1:29pm
"Selective reading" helps. I see clearly:
program that reads integer numbers until you enter zero

and
average numbers that are between 40 and 100


Can you already do the first part?

How does one calculate an average?

What does "Flag Control" mean?
May 5, 2020 at 1:29pm
he wants the condition on the while to trigger from the user input.

something like
int input = -1;
while(input != 0) //do-while would be better here, but you asked for while loop.
{
get input
if input is between 40 and 100 //happily this covers the zero case
add to running total and increment a count
}
average is total/count
Last edited on May 5, 2020 at 1:30pm
May 6, 2020 at 9:34am
i think i found out the solution, here is it to help people around here

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
#include <iostream>

using namespace std;

int main()
{
	int summation=0, mark, count=0; 
	float avg;
	bool loop;
	loop = false;
	cout << "Marks Calculation Program" << endl << endl;

	while (!loop) 
	{
		cout<<"Please Enter Your Marks, input 0 to calculate: ";
		cin >> mark;
		if (mark>=40&&mark<=100) {
			summation+= mark;
			count++;
		}
		else if (mark==0) {
			loop = true;
			cout << "The Loop has ended" << endl << endl << "Your Calculationis: " << endl;
		}
	}
	
	if (count!=0) {
	    cout << "Your Average Mark is: " << (float)summation/count << endl;
	}
	
	else {
	    cout << "Please Enter Vaild Marks." << endl;
	}
	
    return 0;
}
May 6, 2020 at 1:04pm
Good. Couple notes:

A compiler says:
 In function 'int main()':
8:8: warning: unused variable 'avg' [-Wunused-variable]

Some compilers are or can be told to be verbose. Verbose is good. Paying attention to messages is good.
This one is a "warning", the compiler asks: "Are you sure about that?"
You declare a variable "avg", but never use it. Why is it there?

1
2
bool loop;
loop = false;

You declare a variable. Then you set its value. It is preferred to set a value (initialize) in the declaration:
bool loop = false; [EDIT] was missing =
(We know that you know how, for you do it for summation and count.)

while (!loop) means/(can be written too) while (not loop)
The meaning of "loop when not loop" is unexpected. The names of the variables should make sense to a reader.

(float)summation uses C syntax for cast. Casts should be rare and easy to spot.
The C++ cast syntax is more precise and noticeable: static_cast<float>(summation)

The recommended floating point type now is double. The less precise float is for cases, where it is sufficient and/or space counts.

Integer value 0 converts to false. All other integer values convert to true.
Therefore, if (count!=0) and if (count) behave the same.

If I type "hello", the cin >> mark; will fail to get a number and put cin in failed state, where all future reads fail too. In other words, if input data is not plain integers, then the loop will run forever.
If you need to have a flag, then have it, but this would be safer:
1
2
3
int mark;
while ( cin >> mark && mark ) // Stop if read fails or mark==0
{
Last edited on May 12, 2020 at 2:18pm
May 12, 2020 at 12:47pm
i guess that is really helpful for a beginner, thanks alot mate
Topic archived. No new replies allowed.