How to apply if condition in Switch statement, I want to calculate Average: however I have tried for best to solve the issue but still getting no output from Switch statement. I am beginner to the C++,
#include <iostream>
using namespace std;
int main()
{
//declaration of variables.
int sub1,sub2,sub3, sub4,sub5,total,avg;
//accepting marks from user in each subject
cout << " Enter Programming in C++ Marks: " << endl;
cin >> sub1;
cout << " Enter Software Engineering Marks : " << endl;
cin >> sub2;
cout << " Enter Personal Communication Marks : " << endl;
cin >> sub3;
cout << " Enter Database Application Marks: " << endl;
cin >> sub4;
cout << " Enter Computer Concept Marks: " << endl;
cin >> sub5;
//calculatin sum of marks obtained in each subject
total = (sub1 + sub2 + sub3 + sub4 + sub5);
//calculating the average marks
avg = total / 5;
// starting of if condition for finding out grades of total subjects.
switch (avg){
case 1:{
if ((avg >= 80) && (avg <= 100))
{
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A+ " << endl;
}
break;
}
case 2:{
if ((avg >= 70) && (avg <= 79))
{
cout << " Your Average is: " << avg << endl;
cout << " Your grade is A " << endl;
}
break;
}
case 3:{
if ((avg >= 60) && (avg <= 69))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is B+ " << endl;
}
break;
}
case 4:{
if ((avg >= 50) && (avg <= 59))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is C+ " << endl;
}
break;
}
case 5: {
if ((avg >= 40) && (avg <= 49))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is C- ! " << endl;
}
break;
}
case 6: {
if ((avg >= 30) && (avg <= 39))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is D ! " << endl;
}
break;
}
default:{
if ((avg >= 100) && (avg <= 29))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is F, So you are Fail in the class ! " << endl;
break;
}
}
}
system("pause");
}
The switch statement is used to execute one block of code dependent on a particular value. In a sense, the switch statement can be thought of as a form of an if statement: the code
switch (avg) {
case 1 : { /* code block 1 */ } break;
case 2 : { /* code block 2 */ } break;
default : { /* code block default */ } break;
}
can be read as
if (1 == avg) { /* code block 1 */ }
else if (2 == avg) { /* code block 2 */ }
else { /*code block default */ }
Your switch statement can be read as
if (1 == avg)
{
if ((avg >= 80) && (avg <= 100))
{
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A+ " << endl;
}
} else if...
and there's no way avg can == 1 and be greater than 80 and less than 100, which is why you don't get any output.
In C++, the switch statement doesn't lend itself well to testing for ranges; I'd just use an if statement:
if ( (avg<=100) && (avg >=80))
{
// you get an A
} else if ...
But, if you really really need to use a switch, there are a few ways to go about it:
switch (avg) {
case 100:
case 99:
case 98:
...
case 80 : { /* you get an A+ */ break; }
case 79 :
case 78 :
...
case 70 : { /* you get a A */ break: }
etc.
This is ugly, but it is a switch statement. A cleaner way to use the switch statement could be to "force" avg to a discrete value, instead of a range.
int percentile = avg / 10; // integer division.
Now range is going to be between 0 and 10, assuming avg is between 0 and 100. This will make for a slightly cleaner switch:
switch (percentile) {
case 10 :
case 9:
case 8: /* You get an A+ */ break;
case 7: /* You get an A */ break;
case 6: /* You get a B+ */ break;
etc.
Hope this is helpful.
Well, switch compares the cases with the variable you gave it. So for case 1 it tries comparing avg to 1 and after that it would go on with your if statement, which, of course, would never apply.
I'm not really sure what you're trying there; from all I can see you're better off with a nested if:
if ((avg >= 80) && (avg <= 100))
{
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A+ " << endl;
} else if ((avg >= 70) && (avg <= 79))
{
cout << " Your Average is: " << avg << endl;
cout << " Your grade is A " << endl;
} else if ((avg >= 60) && (avg <= 69))
{
cout << " Your Average is: " << avg << endl;
cout << " Your Grade is B+ " << endl;
} ...
But even that can be handled more elegantly, depending on your skills. For example, you can divide the average by 10 and use switch on that, exploiting the fact that integer division truncates, collapsing everything between 50 and 59 into the same bucket 5:
switch (avg / 10) {
case 10:
case 9:
case 8:
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A+ " << endl;
break;
case 7:
cout << " Your Average is: " << avg << endl;
cout << "You Grade is A " << endl;
...
}
Also you'll notice that you print »Your average is: ...« every time, so you can move that line before the conditions:
cout << " Your Average is: " << avg << endl;
switch (avg / 10) {
...
}
Finally, since you have a quite small list of possible outcomes, you can collect them in an array and simply reference the appropriate array element, without even needing an if/else if or switch:
char[][] grades = {
"F", "F", "F", "D", "C-", "C+", "B+", "A", "A+", "A+", "A+"
};
char[] grade;
if (avg > 100)
grade = "F";
else
grade = grades[max(0, avg / 10)];
cout << " Your average is " << avg << endl;
cout << " Your grade is " << grade << endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With