Home > front end >  Why do I get the right output but it also displays the if else output for program 2 and 3
Why do I get the right output but it also displays the if else output for program 2 and 3

Time:04-23

This is a program for admission and there are certain conditions for each program

//The front
cout<<"Welcome to admission system"<<"\n";
cout<<"---------------------------"<<"\n";
cout<<"Admission open for year 2022"<<"\n";
cout<<"Press 1 for BSc Electrical Engineering Department"<<"\n";
cout<<"Press 2 for BSc Mechanical Engineering Department"<<"\n";
cout<<"Press 3 for MSc Mechanical Engineering Department"<<"\n";

//Personal details
cout<<"Your personal details"<<"\n";
cout<<"---------------------"<<"\n";
cout<<"Enter your Civil ID:";
cin>>civil_no;
cout<<"Enter your Name: ";
cin>>name;
cout<<"Enter Age: "<<"\n";
cin>>age;
cout<<"Enter contact Number: ";
cin>>phone;
cout<<"Enter program applying for: ";
cin>>pg; //pg here is meant as the department the user want to choose 

//Education details
cout<<"Education Details"<<"\n";
cout<<"-----------------"<<"\n";
cout<<"Enter Applicant highest qualification passing years:";
//cin>>year_of_passing;
cout<<"Enter Qualification Passing Year:";
//cin>>qualification;
cout<<"Enter Obtained marks (%):";
//cin>>marks;


//Program 1
if (pg==1 && marks>=60 && qualification==12) {
    cout<<"You are fully eligible for this program1"<<"\n";
}
else if(pg==1 || marks<60 || qualification!=12){
    cout<<"you are not eligible 1"<<"\n";
}


//Program 2
if (pg==2 && marks>=65 && qualification==12) {
    cout<<"You are fully eligible for this program 2"<<"\n";
}
else if(pg==2 || marks<65 || qualification!=12){
    cout<<"you are not eligible 2"<<"\n";
}


//Program 3
if (pg==3 && marks>=70 && qualification==14) {
    cout<<"You are fully eligible 3"<<"\n";
}
else if(pg==3 || marks<70 || qualification!=14){
    cout<<"you are not eligible 3"<<"\n";
}

CodePudding user response:

Whether pg==1 or 2 or 3, it can always enter the else condition of the other two programs. For eg, let's say: pg == 1 and marks= 62 and qualification = 12,

1. It will enter program 1's if block

//Program 1
if (pg==1 && marks>=60 && qualification==12) {
    cout<<"You are fully eligible for this program1"<<"\n";
}

2. It will enter program 2's else block

else if(pg==2 || marks<65 || qualification!=12){
    cout<<"you are not eligible 2"<<"\n";
}

3. It will enter program 3's else block

else if(pg==3 || marks<70 || qualification!=14){
    cout<<"you are not eligible 3"<<"\n";
}

This is because in the else block, you have used or || condition due to which even if one condition, (marks in this case) comes out to be true, the block will execute!!

CodePudding user response:

The set of if else statements is incorrect. For example if you entered a value of the qualification not equal to 12 then all three else statements will be executed because the condition qualification!=12 evaluates to true in all three else statements.

You need to write either

//Program 1
if (pg==1 && marks>=60 && qualification==12) {
    cout<<"You are fully eligible for this program1"<<"\n";
}
else if(pg==1 && ( marks<60 || qualification!=12 )){
    cout<<"you are not eligible 1"<<"\n";
}


//Program 2
if (pg==2 && marks>=65 && qualification==12) {
    cout<<"You are fully eligible for this program 2"<<"\n";
}
else if( pg==2 && ( marks<65 || qualification!=12 )){
    cout<<"you are not eligible 2"<<"\n";
}


//Program 3
if (pg==3 && marks>=70 && qualification==14) {
    cout<<"You are fully eligible 3"<<"\n";
}
else if( pg==3 && ( marks<70 || qualification!=14 ) ){
    cout<<"you are not eligible 3"<<"\n";
}

or for example

//Program 1
if ( pg==1 )
{ 
    if ( marks>=60 && qualification==12) {
        cout<<"You are fully eligible for this program1"<<"\n";
    }
    else {
        cout<<"you are not eligible 1"<<"\n";
    }
}


//Program 2
if (pg==2 )
{
    if ( marks>=65 && qualification==12) {
        cout<<"You are fully eligible for this program 2"<<"\n";
    }
    else {
        cout<<"you are not eligible 2"<<"\n";
    }
}


//Program 3
if ( pg==3 )
{
    if ( marks>=70 && qualification==14) {
        cout<<"You are fully eligible 3"<<"\n";
    }
    else {
        cout<<"you are not eligible 3"<<"\n";
    }
}
  • Related