### Activity using switch case

write a program that can compute the grades of the students from pre-mid, midterm, pre-final, final and get the average then identify the average is:
below 74.5 "failed" , above 74.5 - 79.4 "passed" , 80 - 85 "Good" , 86 - 90 "very good" , 91 - 100 "excellent" and above 101 "invalid grades" using switch case statement.

I'm using DevC++.
Last edited on
... and what part of this assignment are you having difficulty with? Have you produced a program design? Do you understand the logic/algorithm required to compute the grades? How would you do this using pen/paper? What exact instructions would you give to someone else who hadn't seen the requirement for them to undertake this? What attempt has been made with the program? If you're produced a program design etc then what C++ issue are you having trouble with? Post your current code for further guidance.
heres what i made but it gave me an error on the switch variable i place.
i want to know why and what went wrong with this program that i made?

 ``123456789101112131415161718192021`` ``````#include using namespace std; int main (){ double a, b, c, d, e; cout<<"Pre-mid Grade: "; cin>>a; cout<<"Midterm Grade: "; cin>>b; cout<<"Pre-final Grade: "; cin>>c; cout<<"Final Grade: "; cin>>d; e=(a+b+c+d)/4; switch (e){ case e>=75.5 && e<=79.4: cout<<"Passed"; break; } }``````
Switch statements are good when you want to check for specific values but are normally less suitable when you want to check for ranges (because they can't). You also can't use them with floating-point numbers (it has to be an integer or enum type).

You could do it with switch statements but then you would have to convert the grades to integers and list all possible values (except you can have a default label which will catch all non-listed values). It's much easier to use if statements for this.
Last edited on
Switch can't be used with a double - nor can it be used with a range. You'll need to use if statement(s). Perhaps:

 ``12345678910111213141516171819202122`` ``````#include int main() { double a {}, b {}, c {}, d {}; std::cout << "Pre-mid Grade: "; std::cin >> a; std::cout << "Midterm Grade: "; std::cin >> b; std::cout << "Pre-final Grade: "; std::cin >> c; std::cout << "Final Grade: "; std::cin >> d; const double e {(a + b + c + d) / 4}; if (e >= 75.5) std::cout << "Passed"; }``````

I didn't know that double can't be used in the switch statement. No wonder it went wrong. Thank you guys for helping this girl out. ^^ I'm a first-year BSIT student by the way.
you can force fit a switch by finding a way to express what you need in integer terms.
some problems, that is really easy to do. Other problems, like this one, its way, way too much work to produce a bloated and slightly convoluted block of code.
So, yes, while if statements look better for this problem, be aware that you can make switches work if necessary.

why would a switch be necessary? Switches and the missing break fall-through concept can reduce complicated logic to much simpler logic. You do not need that here. (Realizing that you do not need it or that forcing the switch would be ugly just comes from experience. If you grow bored and have extra time you could try it and after you get it, the block of junk to get there will be a mess, not because of anything you did, but because of how it must be done).

speaking of that, can you think of a way to avoid testing a range and only test a little each time with if/else?
maybe
blah
... what happens? 95 is > 80, but ... that else prevents doing another term once you found an answer. And you did, its > 91.

BSIT .. the truth finally came out!
Last edited on
There's nothing I can do that's what my prof wants me to do. He didn't explain everything for example I just found out that "double" can't be used in the Switch statement.
 ``1234567891011121314151617181920212223`` ``````#include using namespace std; int main () { double a, b, c, d, e; cout<<"Pre-mid Grade: "; cin>>a; cout<<"Midterm Grade: "; cin>>b; cout<<"Pre-final Grade: "; cin>>c; cout<<"Final Grade: "; cin>>d; e=(a+b+c+d)/4; int f = ( e > 74.5 ) + ( e > 79.5 ) + ( e > 85.5 ) + ( e > 90.5 ) + ( e > 100 ); switch (f) { case 0: cout << "Failed" ; break; case 1: cout << "Passed" ; break; case 2: cout << "Good" ; break; case 3: cout << "Very good"; break; case 4: cout << "Excellent"; break; default: cout << "Invalid"; } }``````

Simpler to use an array of possible outcomes, though.
lastchance wrote:
Simpler to use an array of possible outcomes

Just be careful so that you don't go out of bounds.
Last edited on
 ``1234567891011121314151617`` ``````#include #include using namespace std; int main () { double a, b, c, d, e; cout<<"Pre-mid Grade: "; cin>>a; cout<<"Midterm Grade: "; cin>>b; cout<<"Pre-final Grade: "; cin>>c; cout<<"Final Grade: "; cin>>d; e=(a+b+c+d)/4; int f = ( e > 74.5 ) + ( e > 79.5 ) + ( e > 85.5 ) + ( e > 90.5 ) + ( e > 100 ); // 0 <= f <= 5 string outcomes[] = { "Failed", "Passed", "Good", "Very good", "Excellent", "Invalid" }; cout << outcomes[f] << '\n'; }``````
 ``12345678910111213141516`` ``````#include #include int main() { double a {}, b {}, c {}, d {}; std::cout << "Pre-mid Grade: "; std::cin >> a; std::cout << "Midterm Grade: "; std::cin >> b; std::cout << "Pre-final Grade: "; std::cin >> c; std::cout << "Final Grade: "; std::cin >> d; const auto e {(a + b + c + d) / 4}; const auto f {(e > 74.5) + (e > 79.5) + (e > 85.5) + (e > 90.5) + (e > 100)}; // 0 <= f <= 5 std::cout << *(std::initializer_list{"Failed", "Passed", "Good", "Very good", "Excellent", "Invalid"}.begin() + f) << '\n'; }``````

but the original question requires a switch statement...
Last edited on
Topic archived. No new replies allowed.