[Solved] Averages using loops and if else statements [closed]


Here and there you have made some mistakes.But don’t worry that’s okay.

You have assumed that the student does not take test3 only.But it could be test1 or test2 also.

Another thing to notice is that you have assigned 0 as the score for the test which has not been taken.Again this could create an issue since it could be that the student appeared but scored 0.

I have refined the code and took care of all the corner cases and it does exactly what your instructor wants.

#include<iostream>
using namespace std;

int main()
{
int avg,test1,test2,test3,left=0;
cout<<"Enter the test scores.\n";
cout<<"If a student has not appeared in a test, please enter -1\n";
cout<<"\nTest 1 : ";
cin>>test1;
if(test1==-1)
    left++;

cout<<"\nTest 2 : ";
cin>>test2;
if(test2==-1)
    left++;

cout<<"\nTest 3 : ";
cin>>test3;
if(test3==-1)
    left++;

if(left>1)
{
    cout<<"FAIL : The student has not appeared in more than one tests";
    return 0;
}
else if(left==1)
{
    if(test1==-1)
    {
        avg=(test2+test3)/2;

    }
    else if(test2==-1)
    {
        avg=(test1+test3)/2;
    }
    else if(test3==-1)
    {
        avg=(test1+test2)/2;
    }
}
else if(left==0)
{
    avg=(test1+test2+test3)/3;
}

if(avg>=70 && avg<=100)
{
    cout<<"Excellent !\n";
}
else if(avg>=50 && avg<70)
{
    cout<<"Moderate !\n";
}
else if(avg>=0 && avg<50)
{
    cout<<"Fail !\n";
}
else
{
    cout<<"Internal Computation Error";
    return 0;
}
return 0;
}

7

solved Averages using loops and if else statements [closed]