tags:

views:

161

answers:

7
#include <iostream> 
using namespace std;

int main () 
{
    double testscore;
    cout << " Enter your test score and i will tell you the letter grade you earned ";
    cin >> testscore;
    switch (testscore)
    {
        case (testscore < 60.0):
            cout << "your grade is F.\n";
            break;
        default:
            cout << "that score isnt valid\n.";
    }
return 0;
}
+2  A: 

You can only use switch with integer types. Change your switch to an if.

(and you're missing the closing switch brace.)

+1  A: 

You haven't closed the switch statement block.

Stephen Cox
+3  A: 

There's several problems:

  • You're missing a closing brace } at the end of main(). Not sure if that was intentional, or a cut-and-paste error.

  • The return is inside the switch statement, which means the non-default case will not return the function. Since the function returns an int, this will result in a compilation error.

  • You cannot switch on expressions like that. Use an if statement instead.

LeopardSkinPillBoxHat
Not entirely accurate. Firstly, in C++ a missing `return` statement in a value-returning function is undefined behavior, not a compilation error. Secondly, returning from `main` without an explicit `return` statement is explicitly allowed and defined by the language as equivalent to returning 0.
AndreyT
@AndreyT: Not entirely accurate. A value-returning function *must* contain a return statement! I'm assuming VC++ isn't non-compliant in this regard: **error C4716: 'foo' : must return a value**
@STingRaySC: Well, 6.6.3/2 clearly states that "Flowing off the end of a function is equivalent to a return with no value; this results in undefined behavior in a value-returning function". As I understand, issuing a diagnostic message during translation and terminating translation is a valid way to react to UB-producing code, so there's no reason to say that VC++ is non-compliant in this case. Comeau, on the other hand, issues a mere warning.
AndreyT
A: 

You cannot have an expression in case.

harsha
+1  A: 

You don't allow anyone to pass the class.

Roman A. Taycher
A: 

Some syntax errors and the main point that switch in C can only be used for integer and bool stuff.

#include <iostream> 
using namespace std;

int main () 
{
    double testscore;
    cout << " Enter your test score and i will tell you the letter grade you earned ";
    cin >> testscore;
    if (testscore < 60.0) {
            cout << "your grade is F.\n";
    }
    else {
      cout << "that score isnt valid\n.";
    }

    return 0;
}
Totonga
A: 

switch expects integer expression in the parenthesis which is not the case in your program. Also each case should be integer or constant character expression.

Adil
Pun intended, I assume?