0
WHY the results of these two algorithms are different? please explain in detail, i want to understand
int choose; cout<<"Input 1 for program one"<<"Input 2 for program two"<<endl; cin>>choose; int poin=70; char grade; switch(choose){ case 1:{ if(poin>=0){ grade = 'E'; } if(poin>=46){ grade = 'D'; } if(poin>=56){ grade = 'C'; } if(poin<=100){ grade = 'E'; } cout<<"poin = "<<grade; break; } case 2:{ if(poin>=80){ grade = 'A'; } else if(poin>=66){ grade = 'B'; } else if(poin>=56){ grade = 'C'; } cout<<"poin = "<<grade; break; } } }
1 Answer
+ 1
If this is still relevant, the problem is that you aren't using 'else' in the first case, and your evaluation of 'poin' is wrong. All of the if statements are executed and it finally sets 'grade' to 'C'. Here's what you might have been going for:
case 1:{
if(poin >= 100){
grade = 'E';
}
else if(poin >= 56){
grade = 'C';
}
else if(poin >= 46){
grade = 'D';
}
else if(poin >= 0){
grade = 'E';
}
cout<<"poin = "<<grade;
break;
}
Hope this helps :)