Hello, I have a query regarding c programming. I\'ll be very thankful ifsome one
ID: 3613262 • Letter: H
Question
Hello,
I have a query regarding c programming. I'll be very thankful ifsome one could help. I'm giving the program code below:
//Program that calculates average of student grades.
#include<iostream.h>
#include<stdlib.h>
main()
{
int sum=0, student=0, grade=0, numstudent=0, average=0;
cout<<"Please enter the number of studentsin the class: ";
cin>>numstudent;
cout<<" ******************************************************************************* ";
do
{
cout<<endl<<"Please enter the gradeof student in integer values: ";
cin>>grade;
if(grade<0)
{
break;
}
sum += grade;
student++;
}
while(grade>=0 &&student<numstudent);
cout<<" ******************************************************************************* ";
average = sum / student;
cout<<endl<<"The average of thestudents is: "<<average;
cout<<endl<<endl;
system("pause");
}
When I run this code in Dev C++. The program runs fine. It gives noerror with compilation of code and calculates the average perfectlyas I want it to. But the problem is that the code checks thecondition from the while loop after executing the do loop becauseits a do-while loop and loop executes at least once. What if I wantthe condition to be such that if the user put a value of grade asnegative number (eg: -1). The loop shouldn't execute at all.Because grade cant be a negative number. I've used the breakstatement with the if condition but it wont work. Can anyone helpme with how to solve this problem.
Thanks.