Hey there ! Came back with a new issue today =P

Hello, so as I'm preparing for my programming final which is after tomorrow I'll be on this website for the next 48 hours =D
so here I am with a new code that I need a little help in;

so the question is to read 10 marks entered by the user, their values should be between 0 to 20 , and then calculate and display their sum and avg, YES as easy as that;
now after I've finished the code I've noticed that it's accepting any values rather than values between (0-20), how can I fix that , or where's the mistake in the 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
#include<iostream>

using namespace std;
void main()
{
	float MARK,avg,sum,count;
	sum=0;
	for(count=0;count<10;count++)
	{
		cout<<"Please enter a mark"<<endl;
		cin>>MARK;
		sum = sum+MARK;
	}
		if (MARK>0 || MARK<20)
		{
			
			avg = sum/count;
			cout<<"SUM IS"<<sum<<"AVG IS "<<avg<<endl;
		}
		else
			cout<<"SOrry wrong value entered"<<endl;

system("pause");
}
Last edited on
 
if (MARK > 0 || MARK < 20)


Should be:

 
if (MARK > 0 && MARK < 20)


Edit: If it's inclusive, make it >= and <=.
Last edited on
I tried this problem but in another program and also when I put a mark larger than 20 they accepted !

here my program !
#include <iostream>
using namespace std;
void main()
{
int mark ,count,sum,avg; count=0; sum=0;
while (count<10)
{
do
{
cout<<"Enter amark between 0 and 20"<<endl;
cin>>mark;
}
while (mark<0 || mark>20);
count=count+1;
sum=sum+mark;
}
if (count!=0)
{
avg=sum/count;
cout<<"the average is"<<avg<<endl;
}
system ("pause");
}
and I put (mark>0 && mark<20) it doesn't work !! why that?!
Last edited on
I'd do the validation something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (count = 0; count < 10; count++)
{
   cout << "Please enter a mark: ";
   cin >> mark;

   while (mark < 0 || mark > 20)
   {
      cout << "Invalid mark, please enter again: ";
      cin >> mark;
   } // while

   sum += mark;
} // for 

iHutch105 : THANK YOU! I haven't thought of doing a loop for it ! tried the while loop and it's working correctly now !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
void main()
{
	float MARK,avg,sum,count;
	sum=0;
	for(count=0;count<10;count++)
	{
		cout<<"Please enter a mark"<<endl;
		cin>>MARK;
		 while (MARK< 0 || MARK>20)
		 {
      cout << "Invalid mark, please enter again: ";
      cin >> MARK;
		 }
		sum = sum+MARK;
	}
	
			avg = sum/count;
			cout<<"SUM IS"<<sum<<"AVG IS "<<avg<<endl;

system("pause");
}
Topic archived. No new replies allowed.