#Need help with switch statements

57 messages · Page 1 of 1 (latest)

lethal crow
#

Hi, I'm trying to practice with switch statements and need help. It's not working.

#include <iostream>
#include <iomanip>
#include <cctype>
using namespace std;

float score1, score2, score3, score4, score5, scoreAverage, extraCredit;

cout << "Enter score 1 : ";
cin >> score1;
while (score1 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score1;
}
cout << "Enter score 2 : ";
cin >> score2;
while (score2 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score2;
}
cout << "Enter score 3 : ";
cin >> score3;
while (score3 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score3;
}
cout << "Enter score 4 : ";
cin >> score4;
while (score4 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score4;
}
cout << "Enter score 5 : ";
cin >> score5;
while (score5 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score5;
}
cout << "If you got any extra credit points enter them. If not enter 0 : ";
cin >> extraCredit;
scoreAverage = score1 + score2 + score3 + score4 + score5;
scoreAverage += extraCredit;
scoreAverage = scoreAverage / 5;
cout << scoreAverage;
switch(scoreAverage)
{
    case scoreAverage > 100 : cout << "You did an amazing job!" << endl;
    case scoreAverage >= 90 && scoreAverage <= 100 : cout << "You got an A. Nice job!" << endl;
    case scoreAverage >= 80 && scoreAverage < 90 : cout << "You got a B. Good job!" << endl;
    case scoreAverage >= 70 && scoreAverage < 80 : cout << "You got a C. Decent job. Try studying more." << endl;
    case scoreAverage >= 60 && scoreAverage < 70 : cout << "You got a D. Try studying more." << endl;
    case scoreAverage < 60 : cout << "You got a F. If you need help, please let me know what's wrong." << endl;
}
signal orchidBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

prime vale
#

case statements can't contain conditions

#

only literal values

#

i.e. case 100:

lethal crow
#

Do I have to do case 100 case 99 case 98…..? Sounds like that would take a while

prime vale
#

you can't

#

if you need conditions like that, you need an if

#

I mean, sure, you can write out all possible cases but...

lethal crow
#

Oh god

#

Yeah for this assignment we did this score program with if statements

#

Which I already did

#

But he said to do it again but with switch statements instead of if statements

prime vale
#

"Please enter a valid score"

#

is there a maximum value you can assume?

lethal crow
#

There isn’t really a maximum

#

He said just to make sure there are no values that are less than 0

prime vale
#

also your scores are floats

#

cases can only be used for integers

lethal crow
#

Oh

#

Ok I just changed that

prime vale
#

if you divided the score by 10, then there would be fewer cases you need to write

#

except for all the > 100 (or > 10) numbers 😛

#

an int can usually hold numbers of up to around 2 billion

#

you could use default to catch those cases, though

#

if you handle 0-9

#

for example, say the score is 85
if you divide by 10 (if score is an integer), you get 8

#

so case 8: will cover all scores from 80 to 89

lethal crow
#

ok i see

#

i think i'll try to it like that

lethal crow
#

@prime vale I tried to do what you said but i'm having bit of an issue

#
cout << "Enter score 1 : ";
cin >> score1;
while (score1 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score1;
}
cout << "Enter score 2 : ";
cin >> score2;
while (score2 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score2;
}
cout << "Enter score 3 : ";
cin >> score3;
while (score3 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score3;
}
cout << "Enter score 4 : ";
cin >> score4;
while (score4 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score4;
}
cout << "Enter score 5 : ";
cin >> score5;
while (score5 < 0)
{
    cout << "Please enter a valid score : ";
    cin >> score5;
}
cout << "If you got any extra credit points enter them. If not enter 0 : ";
cin >> extraCredit;
scoreAverage = score1 + score2 + score3 + score4 + score5;
scoreAverage += extraCredit;
scoreAverage = scoreAverage / 5;
cout << scoreAverage << endl;
scoreAverage = scoreAverage / 10;
cout << scoreAverage << endl;
switch(scoreAverage)
{
    case 10 : cout << "You got an A. Nice job!" << endl;
    case 9 : cout << "You got an A. Nice job!" << endl;
    case 8 : cout << "You got a B. Good job!" << endl;
    case 7 : cout << "You got a C. Decent job. Try studying more." << endl;
    case 6 : cout << "You got a D. Try studying more." << endl;
    case 5 : cout << "You got a F. If you need help, please let me know what's wrong." << endl;
    case 4 : cout << "You got a F. If you need help, please let me know what's wrong." << endl;
    case 3: cout << "You got a F. If you need help, please let me know what's wrong." << endl;
    case 2 : cout << "You got a F. If you need help, please let me know what's wrong." << endl;
    case 1 : cout << "You got a F. If you need help, please let me know what's wrong." << endl;
}
#

i'm getting this output

#

You got a C. Decent job. Try studying more.
You got a D. Try studying more.
You got a F. If you need help, please let me know what's wrong.
You got a F. If you need help, please let me know what's wrong.
You got a F. If you need help, please let me know what's wrong.
You got a F. If you need help, please let me know what's wrong.
You got a F. If you need help, please let me know what's wrong.

prime vale
#

you need to break;

#

the switch "jumps" to the matching case, and executes everything from there to the end

#

it "falls through" to the next case

lethal crow
#

would it look like this?

#

case 10 : cout << "You got an A. Nice job!" << endl << break;

#

or is that incorrect?

prime vale
#

break has nothing to do with iostreams

#

it needs to be a separate statement

#

break is a keyword, like switch and case, you can't send it to cout

lethal crow
#

where do i put it?

#

wait i think i know

#

i just pulled up a website

#

i think this would be correct

#

switch(scoreAverage)
{
case 10 :
cout << "You got an A. Nice job!" << endl;
break;

prime vale
#

yeah

#

you can make use of this fall-through behavior, though

#

you can write the stuff you want to happen in the F case just once

#

like

case 4:
case 3:
case 2:
    std::cout << "4, 3, or 2!" << std::endl;
    break;
#

the switch will jump to case 4:, do nothing, and continue until it hits the next break

#

also, your original code behaved a little differently;
a score of 90-100 prints "You got an A. Nice job!",
a score > 100 prints "You did an amazing job!"

#

your new code doesn't do that yet

signal orchidBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.